Help Center/ AI DataLake/ User Guide/ Managing Data and Metadata Using LakeFormation/ Managing Access Credentials/ Obtaining Temporary Credentials of a Flink Job Agency to Access Other Cloud Services
Updated on 2026-11-06 GMT+08:00

Obtaining Temporary Credentials of a Flink Job Agency to Access Other Cloud Services

What Is a Temporary Credential?

Temporary security credentials grant temporary access permissions. They include a temporary access key ID and secret access key (AK/SK) and a security token, which must be used together.

Function Description

AI DataLake provides a standard API to obtain temporary credentials from the job agency. The job agency is configured when an endpoint is created. For details, see Notes and Constraints. This API encapsulates the obtained temporary credentials of the job agency into the com.huaweicloud.sdk.core.auth.BasicCredentials class.

  • The obtained temporary credentials of the agency are encapsulated in the return value of the getCredentials() method of the com.huaweicloud.sdk.core.auth.ICredentialProvider API.
  • The return type is com.huaweicloud.sdk.core.auth.BasicCredentials.
  • Only AKs, SKs, and security tokens can be obtained.
  • The implementation of ICredentialProvider is built into the AI DataLake Flink engine and registered through the SPI mechanism. You do not need to implement this API in your job. Instead, you can directly use the utility class provided in the Sample Code section to load and obtain credentials.

Notes and Constraints

  • Only Flink 1.20 supports using agency authorization to access temporary credentials. The current AI DataLake Flink engine version is Flink 1.20. You do not need to select a job engine version.
  • You have configured the agency (the agency name in the endpoint credential configuration) that allows AI DataLake to access DEW when creating an endpoint. This agency is used when a job obtains temporary credentials.
    • If no job-level agency is specified when a job is submitted, the agency configured for the endpoint is used by default.
    • When submitting a job, you can also specify a job-level agency using the job request parameter job_agency. The specified agency must already exist and cannot be a system management agency.
    • The temporary credentials of the agency are automatically injected into the job execution environment by the platform. You can obtain these credentials through the utility class API in your job code. You do not need to specify agency parameters in the job configuration.

      For agency permission examples, see Creating a Custom Agency and Agency Permission Policies for Common Scenarios.

  • The huaweicloud-sdk-core 3.1.177 is built into the base image of the AI DataLake Flink engine (the version may vary with engine releases). You do not need to install it. Declare the dependency with the provided scope in your POM configuration to prevent it from being packaged into your user JAR file.

Preparing the Environment

Development tools (such as IntelliJ IDEA), JDK, and Maven have been installed and configured.

Dependencies in the POM file configuration

1
2
3
4
5
6
        <dependency>
            <groupId>com.huaweicloud.sdk</groupId>
            <artifactId>huaweicloud-sdk-core</artifactId>
            <version>3.1.177</version>
            <scope>provided</scope>
        </dependency>

To use temporary credentials to access DEW CSMS for secret queries, you must add the huaweicloud-sdk-csms dependency and exclude huaweicloud-sdk-core, which is a transitive dependency of huaweicloud-sdk-csms. This ensures that the SDK core is provided by the engine and prevents duplicate SDK cores from being included in your JAR file.

        <dependency>
            <groupId>com.huaweicloud.sdk</groupId>
            <artifactId>huaweicloud-sdk-csms</artifactId>
            <version>3.1.177</version>
            <exclusions>
                <exclusion>
                    <groupId>com.huaweicloud.sdk</groupId>
                    <artifactId>huaweicloud-sdk-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

Sample Code

This section provides Java code examples demonstrating how to obtain BasicCredentials, along with the AK, SK, and security token of a job agency.

Replace CSMS_ENDPOINT, PROJECT_ID, and SECRET_NAME in the sample code with your actual values.

  • CSMS_ENDPOINT: Endpoint for accessing Huawei Cloud CSMS. The endpoint corresponds to the region where the target secret is located. The format is https://kms.<region>.myhuaweicloud.com. You can find endpoints in DEW API Reference > Before You Start > Endpoints.
  • PROJECT_ID: Project ID of the Huawei Cloud region. Obtain the project ID for the region corresponding to CSMS_ENDPOINT. You can view your project ID by clicking your username in the upper right corner of the console, choosing My Credentials, and viewing the project list.
  • SECRET_NAME: Name of the target secret in DEW.
  • Obtaining job agency credentials using Flink UDFs
    package com.aidatalake.flink.demo;
    
    import static com.aidatalake.flink.demo.utils.AIDataLakeJobAgencyCredentialUtils.getICredentialProvider;
    
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.auth.ICredentialProvider;
    import com.huaweicloud.sdk.csms.v1.CsmsClient;
    import com.huaweicloud.sdk.csms.v1.model.Secret;
    import com.huaweicloud.sdk.csms.v1.model.ShowSecretRequest;
    import com.huaweicloud.sdk.csms.v1.model.ShowSecretResponse;
    
    import org.apache.flink.table.functions.FunctionContext;
    import org.apache.flink.table.functions.ScalarFunction;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.util.List;
    
    public class GetUserAgencyCredentialUDF extends ScalarFunction {
        private static final Logger LOG = LoggerFactory.getLogger(GetUserAgencyCredentialUDF.class);
    
        // Replace the DEW CSMS access address and project ID with your actual values.
        private static final String CSMS_ENDPOINT = "https://kms.<region>.myhuaweicloud.com";
        private static final String PROJECT_ID = "your-project-id";
    
        ICredentialProvider credentialProvider;
        CsmsClient csmsClient;
    
        @Override
        public void open(FunctionContext context) throws Exception {
            // Obtain the temporary credentials of the job agency through the SPI. You do not need to specify agency parameters in the job configuration.
            credentialProvider = getICredentialProvider();
    
            // The security token and temporary AK/SK are encapsulated in BasicCredentials (a security token is mandatory for temporary credentials). Specify your project ID and build the CSMS client.
            BasicCredentials basicCredentials = (BasicCredentials) credentialProvider.getCredentials();
            basicCredentials = basicCredentials.withProjectId(PROJECT_ID);
            csmsClient = CsmsClient.newBuilder()
                    .withCredential(basicCredentials)
                    .withEndpoints(List.of(CSMS_ENDPOINT))
                    .build();
        }
    
        public String eval(String secretName) {
            ShowSecretResponse response = csmsClient.showSecret(
                    new ShowSecretRequest().withSecretName(secretName));
            Secret secret = response.getSecret();
            if (secret == null) {
                throw new RuntimeException("DEW secret not found: " + secretName);
            }
            LOG.info("Accessed DEW CSMS with agency temp credentials, secret name={}", secret.getName());
            // The returned credential metadata indicates that DEW was successfully accessed using the temporary credentials of the job agency. In production services, you can retrieve the secret value here for subsequent access to other cloud services.
            return secret.getName();
        }
    }

    After packaging the UDF class and utility class into a JAR file and uploading it to OBS, you can register the UDF in a Flink SQL job using the CREATE TEMPORARY FUNCTION statement and use the temporary credentials of the job agency to query DEW secrets in a SELECT statement.

    CREATE TEMPORARY FUNCTION get_dew_credential AS 'com.aidatalake.flink.demo.GetUserAgencyCredentialUDF'
      USING JAR 'obs://<bucket>/<path>/agency-credential-udf.jar';
    SELECT get_dew_credential('secret-name');

    The JAR file specified by USING JAR is automatically downloaded and registered during SQL compilation. You do not need to add dependencies in the job configuration. A successful job execution indicates that the system successfully used the job agency temporary credentials to access DEW.

  • Obtaining job agency credentials for Flink JAR jobs
    package com.aidatalake.flink.demo;
    
    import static com.aidatalake.flink.demo.utils.AIDataLakeJobAgencyCredentialUtils.getICredentialProvider;
    
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.auth.ICredentialProvider;
    import com.huaweicloud.sdk.csms.v1.CsmsClient;
    import com.huaweicloud.sdk.csms.v1.model.Secret;
    import com.huaweicloud.sdk.csms.v1.model.ShowSecretRequest;
    import com.huaweicloud.sdk.csms.v1.model.ShowSecretResponse;
    
    import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
    
    import java.util.List;
    
    public class GetUserCredentialsFlinkStream {
        // Replace the DEW CSMS access address, project ID, and secret name with your actual values.
        private static final String CSMS_ENDPOINT = "https://kms.<region>.myhuaweicloud.com";
        private static final String PROJECT_ID = "your-project-id";
        private static final String SECRET_NAME = "your-secret-name";
    
        public static void main(String[] args) throws Exception {
            StreamExecutionEnvironment streamEnv = StreamExecutionEnvironment.getExecutionEnvironment();
    
            streamEnv.fromElements(1)
                    .map(i -> {
                        ICredentialProvider credentialProvider = getICredentialProvider();
                        // The security token and temporary AK/SK are encapsulated in BasicCredentials (a security token is mandatory for temporary credentials). Specify your project ID and build the CSMS client.
                        BasicCredentials basicCredentials = (BasicCredentials) credentialProvider.getCredentials();
                        basicCredentials = basicCredentials.withProjectId(PROJECT_ID);
    
                        CsmsClient client = CsmsClient.newBuilder()
                                .withCredential(basicCredentials)
                                .withEndpoints(List.of(CSMS_ENDPOINT))
                                .build();
    
                        // Use the temporary credentials of the job agency to query the DEW secret.
                        ShowSecretResponse response = client.showSecret(
                                new ShowSecretRequest().withSecretName(SECRET_NAME));
                        Secret secret = response.getSecret();
                        if (secret == null) {
                            throw new RuntimeException("DEW secret not found: " + SECRET_NAME);
                        }
                        return "Accessed DEW CSMS with job agency credential, secret name=" + secret.getName()
                                + ", id=" + secret.getId();
                    })
                    .print();
    
            streamEnv.execute("GetUserCredentialsFlinkStream");
        }
    }
  • Obtaining the utility class for job agencies
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    package com.aidatalake.flink.demo.utils;
    
    import com.huaweicloud.sdk.core.auth.ICredentialProvider;
    
    import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.ServiceLoader;
    
    public class AIDataLakeJobAgencyCredentialUtils {
    
        public static ICredentialProvider getICredentialProvider() {
            List<ICredentialProvider> credentialProviders = new ArrayList<>();
            ServiceLoader.load(ICredentialProvider.class, StreamExecutionEnvironment.class.getClassLoader())
                .iterator()
                .forEachRemaining(credentialProviders::add);
    
            if (credentialProviders.size() != 1) {
                throw new RuntimeException("Failed to obtain temporary user credential");
            }
            return credentialProviders.get(0);
        }
    }