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

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

Function

AI DataLake Spark provides a standard API to obtain temporary credentials from the Spark job agency you configured when starting a job. 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.huawei.spark.provider.DewProvider API.
  • The return type is com.huaweicloud.sdk.core.auth.BasicCredentials.
  • Only AKs, SKs, and security tokens can be obtained.
  • After obtaining the AK, SK, and security token, query temporary credentials by referring to Using CSMS to Change Hard-Coded Database Account Passwords.

Notes and Constraints

  • Only Spark 4.0 (Spark general queue scenario) can use agency authorization to access temporary credentials.
    • When creating a Spark job, use Spark version 4.0.
    • You have configured the agency information that allows access to DEW in the job.

      For details about how to create a custom agency, see Creating a Custom Agency.

      Note that configuration parameters do not need to be enclosed in double quotation marks ("") or single quotation marks ('').

  • The Spark 4.0 base image includes huaweicloud-sdk-core 3.1.62. You do not need to reinstall it.

Preparing the Environment

You have installed and configured IntelliJ IDEA, JDK, and Maven.

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.62</version>
    <scope>provided</scope>
</dependency>

Example Code

This section provides Java code examples to show how to obtain BasicCredentials and the AK, SK, and security token of a temporary agency.

  • Obtaining job agency credentials for Spark JAR jobs
    import com.huaweicloud.sdk.core.auth.ICredentialProvider;
    import org.apache.spark.sql.SparkSession;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.ServiceLoader;
    
    public class JobAgencyCredentialUtils {
        // Load all credential providers via the SPI and filter for the DewProvider instance.
        public static ICredentialProvider getICredentialProvider() {
            List<ICredentialProvider> credentialProviders = new ArrayList<>();
            ServiceLoader.load(ICredentialProvider.class, SparkSession.class.getClassLoader())
                    .iterator()
                    .forEachRemaining(credentialProviders::add);
            if (credentialProviders.isEmpty()) {
                throw new RuntimeException("No ICredentialProvider found");
            }
            return credentialProviders.stream()
                    .filter(p -> p.getClass().getName().equals("com.huawei.spark.provider.DewProvider"))
                    .findFirst()
                    .orElseThrow(() -> new RuntimeException("DewProvider not found"));
        }
    }
  • Obtaining the utility class for job agencies
    import org.apache.spark.sql.SparkSession;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.auth.ICredentialProvider;
    
    import static org.example.JobAgencyCredentialUtils.getICredentialProvider;
    
    public class GetUserCredentialsSparkJar {
        public static void main(String[] args) throws Exception {
            SparkSession spark = SparkSession
                    .builder()
                    .appName("test_spark")
                    .getOrCreate();
            ICredentialProvider credentialProvider = getICredentialProvider();
            BasicCredentials basicCredentials = (BasicCredentials) credentialProvider.getCredentials();
            String ak = basicCredentials.getAk();
            String sk = basicCredentials.getSk();
            String securityToken = basicCredentials.getSecurityToken();
            //
            System.out.println("ak:" + ak);
            System.out.println("sk:" + sk);
            System.out.println("securityToken:" + securityToken);
            spark.stop();
        }
    }