Skip to content

Commit

Permalink
Rename method
Browse files Browse the repository at this point in the history
  • Loading branch information
Mats Faugli committed Nov 20, 2020
1 parent 3bfb810 commit 9d326af
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static byte[] base64Decode(String s) {
* @param s the secret whose scope we want to obtain.
* @return the scope for a given secret.
*/
public static CredentialsScope getScope(Secret s) {
public static CredentialsScope getCredentialScope(Secret s) {
return CredentialsScope.valueOf(Optional
.ofNullable(s.getMetadata().getLabels().get(JENKINS_IO_CREDENTIALS_SCOPE_LABEL))
.orElse("global")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.CredentialsConvertionException;
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.SecretToCredentialConverter;
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.SecretUtils;
import com.cloudbees.plugins.credentials.CredentialsScope;

/**
* SecretToCredentialConvertor that converts {@link AWSCredentialsImpl}.
Expand Down Expand Up @@ -72,7 +71,7 @@ public AWSCredentialsImpl convert(Secret secret) throws CredentialsConvertionExc

return new AWSCredentialsImpl(
// Scope
SecretUtils.getScope(secret),
SecretUtils.getCredentialScope(secret),
// ID
SecretUtils.getCredentialId(secret),
// AccessKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.CredentialsConvertionException;
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.SecretToCredentialConverter;
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.SecretUtils;
import com.cloudbees.plugins.credentials.CredentialsScope;

/**
* SecretToCredentialConvertor that converts {@link BasicSSHUserPrivateKey}.
Expand Down Expand Up @@ -63,7 +62,7 @@ public BasicSSHUserPrivateKey convert(Secret secret) throws CredentialsConvertio

return new BasicSSHUserPrivateKey(
// Scope
SecretUtils.getScope(secret),
SecretUtils.getCredentialScope(secret),
// ID
SecretUtils.getCredentialId(secret),
// Username
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.CredentialsConvertionException;
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.SecretToCredentialConverter;
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.SecretUtils;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.SecretBytes;
import com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl;

Expand Down Expand Up @@ -58,7 +57,7 @@ public CertificateCredentialsImpl convert(Secret secret) throws CredentialsConve
byte[] certData = SecretUtils.requireNonNull(SecretUtils.base64Decode(certBase64), "certificate credential has an invalid certificate (must be base64 encoded data)");
SecretBytes sb = SecretBytes.fromBytes(certData);

CertificateCredentialsImpl certificateCredentialsImpl = new CertificateCredentialsImpl(SecretUtils.getScope(secret), SecretUtils.getCredentialId(secret), SecretUtils.getCredentialDescription(secret), password, new CertificateCredentialsImpl.UploadedKeyStoreSource(sb));
CertificateCredentialsImpl certificateCredentialsImpl = new CertificateCredentialsImpl(SecretUtils.getCredentialScope(secret), SecretUtils.getCredentialId(secret), SecretUtils.getCredentialDescription(secret), password, new CertificateCredentialsImpl.UploadedKeyStoreSource(sb));
try {
if (certificateCredentialsImpl.getKeyStore().size() == 0) {
throw new CredentialsConvertionException("certificate credential has an invalid certificate (encoded data is not a valid PKCS#12 format certificate understood by Java)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.CredentialsConvertionException;
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.SecretToCredentialConverter;
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.SecretUtils;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.SecretBytes;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;

Expand Down Expand Up @@ -58,7 +57,7 @@ public FileCredentialsImpl convert(Secret secret) throws CredentialsConvertionEx
byte[] _data = SecretUtils.requireNonNull(SecretUtils.base64Decode(dataBase64), "secretFile credential has an invalid data (must be base64 encoded data)");

SecretBytes sb = SecretBytes.fromBytes(_data);
return new FileCredentialsImpl(SecretUtils.getScope(secret), SecretUtils.getCredentialId(secret), SecretUtils.getCredentialDescription(secret), filename, sb);
return new FileCredentialsImpl(SecretUtils.getCredentialScope(secret), SecretUtils.getCredentialId(secret), SecretUtils.getCredentialDescription(secret), filename, sb);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.CredentialsConvertionException;
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.SecretToCredentialConverter;
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.SecretUtils;
import com.cloudbees.plugins.credentials.CredentialsScope;

import io.fabric8.kubernetes.api.model.Secret;

Expand Down Expand Up @@ -65,7 +64,7 @@ public OpenstackCredentialv3 convert(Secret secret) throws CredentialsConvertion
String passwordBase64 = SecretUtils.getNonNullSecretData(secret, "password", "openstackCredentialv3 credential is missing the password");
String password = SecretUtils.requireNonNull(SecretUtils.base64DecodeToString(passwordBase64), "openstackCredentialv3 credential has an invalid password (must be base64 encoded UTF-8)");

return new OpenstackCredentialv3(SecretUtils.getScope(secret), SecretUtils.getCredentialId(secret), SecretUtils.getCredentialDescription(secret), userName, userDomain, projectName, projectDomain, password);
return new OpenstackCredentialv3(SecretUtils.getCredentialScope(secret), SecretUtils.getCredentialId(secret), SecretUtils.getCredentialDescription(secret), userName, userDomain, projectName, projectDomain, password);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.CredentialsConvertionException;
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.SecretToCredentialConverter;
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.SecretUtils;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;

/**
Expand All @@ -52,7 +51,7 @@ public StringCredentialsImpl convert(Secret secret) throws CredentialsConvertion

String secretText = SecretUtils.requireNonNull(SecretUtils.base64DecodeToString(textBase64), "secretText credential has an invalid text (must be base64 encoded UTF-8)");

return new StringCredentialsImpl(SecretUtils.getScope(secret), SecretUtils.getCredentialId(secret), SecretUtils.getCredentialDescription(secret), hudson.util.Secret.fromString(secretText));
return new StringCredentialsImpl(SecretUtils.getCredentialScope(secret), SecretUtils.getCredentialId(secret), SecretUtils.getCredentialDescription(secret), hudson.util.Secret.fromString(secretText));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.CredentialsConvertionException;
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.SecretToCredentialConverter;
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.SecretUtils;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;

/**
Expand All @@ -54,7 +53,7 @@ public UsernamePasswordCredentialsImpl convert(Secret secret) throws Credentials

String password = SecretUtils.requireNonNull(SecretUtils.base64DecodeToString(passwordBase64), "usernamePassword credential has an invalid password (must be base64 encoded UTF-8)");

return new UsernamePasswordCredentialsImpl(SecretUtils.getScope(secret), SecretUtils.getCredentialId(secret), SecretUtils.getCredentialDescription(secret), username, password);
return new UsernamePasswordCredentialsImpl(SecretUtils.getCredentialScope(secret), SecretUtils.getCredentialId(secret), SecretUtils.getCredentialDescription(secret), username, password);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.api.model.SecretBuilder;
import org.junit.Test;
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.CredentialsConvertionException;
import com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.SecretUtils;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.text.StringContainsInOrder.stringContainsInOrder;
Expand Down Expand Up @@ -77,14 +76,14 @@ public void base64DecodeWithValidInput() {
public void getCredentialScope() {
Map<String, String> scopeLabel = Collections.singletonMap(SecretUtils.JENKINS_IO_CREDENTIALS_SCOPE_LABEL, "system");
Secret s = new SecretBuilder().withNewMetadata().withLabels(scopeLabel).endMetadata().build();
assertThat(SecretUtils.getScope(s), is(CredentialsScope.SYSTEM));
assertThat(SecretUtils.getCredentialScope(s), is(CredentialsScope.SYSTEM));
}

@Test
public void getCredentialScopeDefaultsToGlobal() {
Map<String, String> typeLabel = Collections.singletonMap(SecretUtils.JENKINS_IO_CREDENTIALS_TYPE_LABEL, "usernamePassword");
Secret s = new SecretBuilder().withNewMetadata().withLabels(typeLabel).endMetadata().build();
assertThat(SecretUtils.getScope(s), is(CredentialsScope.GLOBAL));
assertThat(SecretUtils.getCredentialScope(s), is(CredentialsScope.GLOBAL));
}

@Test
Expand Down

0 comments on commit 9d326af

Please sign in to comment.