Class SecretUtils


  • public abstract class SecretUtils
    extends Object
    Collection of utilities for working with Secrets. Note: API may be subject to change.
    • Constructor Detail

      • SecretUtils

        public SecretUtils()
    • 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 given Secret. 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 given Secret.
        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 given Secret.
        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 that obj is not null.
        Type Parameters:
        T - the type of the obj.
        Parameters:
        obj - the Object to check for null.
        exceptionMessage - detail message to be used in the event that a CredentialsConvertionException is thrown.
        Returns:
        obj if not null.
        Throws:
        CredentialsConvertionException - iff obj is null.
      • requireNonNull

        public static <T> T requireNonNull​(@Nullable
                                           T obj,
                                           String exceptionMessage,
                                           @Nullable
                                           String mapped)
                                    throws CredentialsConvertionException
        Checks that obj is not null.
        Type Parameters:
        T - the type of the obj.
        Parameters:
        obj - the Object to check for null.
        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 not null.
        Throws:
        CredentialsConvertionException - iff obj is null.
      • 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 Secret
        key - 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 Secret
        key - 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 type jenkins.io/credentials-keybinding-name containing the custom name - for example jenkins.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.