Class SecretUtils
- java.lang.Object
-
- com.cloudbees.jenkins.plugins.kubernetes_credentials_provider.SecretUtils
-
public abstract class SecretUtils extends Object
Collection of utilities for working withSecret
s. Note: API may be subject to change.
-
-
Constructor Summary
Constructors Constructor Description SecretUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]
base64Decode(String s)
Convert a String representation of the base64 encoded bytes back to a byte[].static String
base64DecodeToString(String s)
Convert a String representation of the base64 encoded bytes of a UTF-8 String back to a String.static String
getCredentialDescription(io.fabric8.kubernetes.api.model.Secret s)
Obtain the credential description from a givenSecret
.static String
getCredentialId(io.fabric8.kubernetes.api.model.Secret s)
Obtain the credential ID from a givenSecret
.static Set<String>
getCredentialItemScopes(io.fabric8.kubernetes.api.model.Secret s)
Gets all item-group scopes from a secret.static com.cloudbees.plugins.credentials.CredentialsScope
getCredentialScope(io.fabric8.kubernetes.api.model.Secret s)
Get the scope from a givenSecret
.static String
getKeyName(io.fabric8.kubernetes.api.model.Secret s, String key)
Get the mapping for the specified key name.static String
getNonNullSecretData(io.fabric8.kubernetes.api.model.Secret s, String key, String exceptionMessage)
Get the data for the specified key (or the mapped key if key is mapped), or throw a CredentialsConvertionException if the data for the given key was not present..static Optional<String>
getOptionalSecretData(io.fabric8.kubernetes.api.model.Secret s, String key, String exceptionMessage)
Get optional data for the specified key (or the mapped key if key is mapped)static <T> T
requireNonNull(T obj, String exceptionMessage)
Checks thatobj
is notnull
.static <T> T
requireNonNull(T obj, String exceptionMessage, String mapped)
Checks thatobj
is notnull
.
-
-
-
Method Detail
-
base64DecodeToString
@CheckForNull @Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static String base64DecodeToString(String s)
Convert a String representation of the base64 encoded bytes of a UTF-8 String back to a String.- Parameters:
s
- the base64 encoded String representation of the bytes.- Returns:
- the String or
null
if the string could not be converted.
-
base64Decode
@CheckForNull @Restricted(org.kohsuke.accmod.restrictions.NoExternalUse.class) public static byte[] base64Decode(String s)
Convert a String representation of the base64 encoded bytes back to a byte[].- Parameters:
s
- the base64 encoded representation of the bytes.- Returns:
- the byte[] or
null
if the string could not be converted.
-
getCredentialScope
public static com.cloudbees.plugins.credentials.CredentialsScope getCredentialScope(io.fabric8.kubernetes.api.model.Secret s) throws CredentialsConvertionException
Get the scope from a givenSecret
. If the label is empty, then it defaults to global scope.- Parameters:
s
- the secret whose scope we want to obtain.- Returns:
- the scope for a given secret.
- Throws:
CredentialsConvertionException
- if scope is invalid.
-
getCredentialItemScopes
public static Set<String> getCredentialItemScopes(io.fabric8.kubernetes.api.model.Secret s)
Gets all item-group scopes from a secret. Format of the annotation: "['thisIsJobA', 'thisIsJobB', 'thisIsJobC/thisIsJobD']"- Parameters:
s
- the secret whose item-group scope we want to obtain.- Returns:
- a set of all item-group scopes
-
getCredentialId
public static String getCredentialId(io.fabric8.kubernetes.api.model.Secret s)
Obtain the credential ID from a givenSecret
.- Parameters:
s
- the secret whose id we want to obtain.- Returns:
- the credential ID for a given secret.
-
getCredentialDescription
@CheckForNull public static String getCredentialDescription(io.fabric8.kubernetes.api.model.Secret s)
Obtain the credential description from a givenSecret
.- Parameters:
s
- the secret whose description we want to obtain.- Returns:
- the credential description for a given secret.
-
requireNonNull
public static <T> T requireNonNull(@Nullable T obj, String exceptionMessage) throws CredentialsConvertionException
Checks thatobj
is notnull
.- Type Parameters:
T
- the type of the obj.- Parameters:
obj
- the Object to check fornull
.exceptionMessage
- detail message to be used in the event that a CredentialsConvertionException is thrown.- Returns:
obj
if notnull
.- Throws:
CredentialsConvertionException
- iffobj
isnull
.
-
requireNonNull
public static <T> T requireNonNull(@Nullable T obj, String exceptionMessage, @Nullable String mapped) throws CredentialsConvertionException
Checks thatobj
is notnull
.- Type Parameters:
T
- the type of the obj.- Parameters:
obj
- the Object to check fornull
.exceptionMessage
- detail message to be used in the event that a CredentialsConvertionException is thrown.mapped
- an optional mapping (adds a"mapped to " + mapped
to the exception message if this is non null.- Returns:
obj
if notnull
.- Throws:
CredentialsConvertionException
- iffobj
isnull
.
-
getNonNullSecretData
public static String getNonNullSecretData(io.fabric8.kubernetes.api.model.Secret s, String key, String exceptionMessage) throws CredentialsConvertionException
Get the data for the specified key (or the mapped key if key is mapped), or throw a CredentialsConvertionException if the data for the given key was not present..- Parameters:
s
- the Secretkey
- the key to get the data for (which may be mapped to another key).exceptionMessage
- the detailMessage of the exception if the data for the key (or mapped key) was not present.- Returns:
- The data for the given key.
- Throws:
CredentialsConvertionException
- if the data was not present.
-
getOptionalSecretData
public static Optional<String> getOptionalSecretData(io.fabric8.kubernetes.api.model.Secret s, String key, String exceptionMessage) throws CredentialsConvertionException
Get optional data for the specified key (or the mapped key if key is mapped)- Parameters:
s
- the Secretkey
- the key to get the data for (which may be mapped to another key).exceptionMessage
- the detailMessage of the exception if the data for the key (or mapped key) was not present.- Returns:
- Optional data for specified key
- Throws:
CredentialsConvertionException
- if the data was not present.
-
getKeyName
public static String getKeyName(io.fabric8.kubernetes.api.model.Secret s, String key)
Get the mapping for the specified key name. Secrets can override the defaults used by the plugin by specifying an attribute of the typejenkins.io/credentials-keybinding-name
containing the custom name - for examplejenkins.io/credentials-keybinding-foo=wibble
.- Parameters:
s
- the secret to inspect for a custom name.key
- the name of the key we are looking for.- Returns:
- the custom mapping for the key or
key
(identical object) if there is no custom mapping.
-
-