Using DEW to Acquire Access Credentials for OBS Read and Write in Flink JAR
Scenarios
When AI DataLake writes output data from Flink JAR jobs to OBS, you must configure an AK/SK to access OBS. To ensure AK/SK security, you can use DEW and CSMS to manage AK/SK credentials centrally. This approach effectively mitigates risks such as sensitive information leakage caused by hardcoding in code or plaintext configurations, as well as potential business risks due to unauthorized access.
This section uses obtaining AK/SK to access OBS as an example to explain how to use DEW to acquire access credentials for Flink JAR to read and write OBS data.
Notes and Constraints
DEW can be used to manage access credentials only in Flink 1.20. When creating a Flink job, select version 1.20 and configure the agency details that authorize AI DataLake to access DEW.
Prerequisites
- A shared secret has been created on the DEW console, and its value has been stored. For details, see Creating a Shared Secret.
- To use this feature, you must configure the AK/SK for all involved OBS buckets.
- Ensure that you have created an agency that allows AI DataLake to access DEW and completed the corresponding agency authorization. The agency must have the following permissions:
- ShowSecretVersion permission for querying secret versions and secret values in DEW: csms:secretVersion:get
- ListSecretVersions permission for listing secret versions in DEW: csms:secretVersion:list
- Permission to decrypt DEW secrets: kms:dek:decrypt
For agency permission examples, see Creating a Custom Agency and Agency Permission Policies for Common Scenarios.
Syntax
When submitting a job using the Flink job creation API, configure the DEW access credential in the flink_config field as follows:
- Global configuration (valid for all OBS buckets):
1 2 3 4 5 6 7
flink.hadoop.fs.obs.dew.access.key=USER_AK_CSMS_KEY flink.hadoop.fs.obs.dew.secret.key=USER_SK_CSMS_KEY flink.hadoop.fs.obs.dew.session.token=USER_TOKEN_CSMS_KEY flink.hadoop.fs.obs.security.provider=com.aidatalake.flink.auth.provider.DewObsCredentialProvider flink.hadoop.fs.dew.csms.secretName=CredentialName flink.hadoop.fs.dew.csms.version=VERSION_ID flink.hadoop.fs.dew.csms.cache.time.second=CACHE_TIME
- Bucket-specific configuration (valid only for specified buckets; different OBS buckets can use different AK/SK authentication credentials):
1 2 3 4 5 6 7
flink.hadoop.fs.obs.bucket.USER_BUCKET_NAME.dew.access.key=USER_AK_CSMS_KEY flink.hadoop.fs.obs.bucket.USER_BUCKET_NAME.dew.secret.key=USER_SK_CSMS_KEY flink.hadoop.fs.obs.bucket.USER_BUCKET_NAME.dew.session.token=USER_TOKEN_CSMS_KEY flink.hadoop.fs.obs.bucket.USER_BUCKET_NAME.security.provider=com.aidatalake.flink.auth.provider.DewObsCredentialProvider flink.hadoop.fs.dew.csms.secretName=CredentialName flink.hadoop.fs.dew.csms.version=VERSION_ID flink.hadoop.fs.dew.csms.cache.time.second=CACHE_TIME
Bucket-specific configurations take precedence over global configurations. When accessing an OBS bucket, if bucket-specific configurations are available for that bucket, they are used first. Parameters that are not configured on a bucket-specific basis fall back to the global configurations.
Bucket-specific configurations take effect for all OBS operations accessing the specified bucket. If the job JAR package and dependency files are stored in this bucket, the credential corresponding to the bucket is also used when the system pulls the job files. Ensure that this credential has the required read permissions.
Parameters starting with flink.hadoop.fs.dew.csms support only global configurations. Replace USER_BUCKET_NAME with the name of your OBS bucket. For details about the parameters, see Table 1.
Note: The endpoint and project ID of DEW are automatically injected by the AI DataLake platform during job execution. You do not need to configure them. Jobs access DEW using the temporary credentials of the configured job agency. Therefore, the agency must have the permission to query CSMS secrets.
Parameter Description
| Parameter | Mandatory | Default Value | Data Type | Description |
|---|---|---|---|---|
| flink.hadoop.fs.obs.dew.access.key | Yes | None | String | This parameter can be configured in two modes: global configuration flink.hadoop.fs.obs.dew.access.key (valid for all OBS buckets) and bucket-specific configuration flink.hadoop.fs.obs.bucket.USER_BUCKET_NAME.dew.access.key (valid only for the specified bucket, where USER_BUCKET_NAME must be replaced with the actual OBS bucket name). Bucket-specific configurations take precedence over global configurations. The value of this parameter is the key you defined in the CSMS shared secret. The value corresponding to this key is your access key ID (AK). You must have the permission to access the specified OBS bucket. |
| flink.hadoop.fs.obs.bucket.dew.secret.key | Yes | None | String | This parameter can be configured in two modes: global configuration flink.hadoop.fs.obs.dew.secret.key (valid for all OBS buckets) and bucket-specific configuration flink.hadoop.fs.obs.bucket.USER_BUCKET_NAME.dew.secret.key (valid only for the specified bucket, where USER_BUCKET_NAME must be replaced with the actual OBS bucket name). Bucket-specific configurations take precedence over global configurations. The value of this parameter is the key you defined in the CSMS shared secret. The value corresponding to the key is your secret access key (SK). You must have the permission to access the specified OBS bucket. |
| flink.hadoop.fs.obs.bucket.dew.session.token | No | None | String | This parameter can be configured in two modes: global configuration flink.hadoop.fs.obs.dew.session.token (valid for all OBS buckets) and bucket-specific configuration flink.hadoop.fs.obs.bucket.USER_BUCKET_NAME.dew.session.token (valid only for the specified bucket, where USER_BUCKET_NAME must be replaced with the actual OBS bucket name). Bucket-specific configurations take precedence over global configurations. The parameter value is the key you defined in the CSMS shared secret, and the value corresponding to this key is your temporary access credential (security token). This parameter is required only when the AK/SK stored in the shared secret is a temporary credential. If a permanent AK/SK is used, you do not need to set this parameter. |
| flink.hadoop.fs.obs.security.provider | Yes | None | String | The OBS AK/SK authentication mechanism, which uses the secret management of CSMS, a sub-service of DEW, to obtain the AK and SK for accessing OBS. When DEW is used to access OBS, the value is fixed to com.aidatalake.flink.auth.provider.DewObsCredentialProvider. This parameter can be configured in two modes: global configuration flink.hadoop.fs.obs.security.provider (valid for all OBS buckets) and bucket-specific configuration flink.hadoop.fs.obs.bucket.USER_BUCKET_NAME.security.provider (valid only for the specified bucket, where USER_BUCKET_NAME must be replaced with the actual OBS bucket name). Bucket-specific configurations take precedence over global configurations. If this parameter is not set, the temporary credentials of the job agency are used to access OBS by default. |
| flink.hadoop.fs.dew.csms.secretName | Yes | None | String | The name of the shared secret created in DEW secret management. Configuration example: flink.hadoop.fs.dew.csms.secretName=secretInfo |
| flink.hadoop.fs.dew.csms.cache.time.second | No | 3600 | Long | The cache duration after a Flink job obtains a CSMS shared secret. The unit is seconds. The default value is 3600 seconds. The value must be a non-negative integer. Otherwise, the job fails to start. If this parameter is set to 0, the system retrieves credentials from CSMS every time OBS is accessed. |
Sample Code
This section describes how to write processed DataGen data to OBS. You need to modify the parameters in the sample Java code to match your service requirements.
- Create an agency for AI DataLake to access DEW, and complete the agency authorization. For details, see Creating a Custom Agency.
- Create a shared secret in DEW. For details, see Creating a Shared Secret.
The secret value is the OBS AK/SK information saved in JSON format. The key name can be customized, but it must match the values of dew.access.key and dew.secret.key configured in the optimization parameters. For example:
{"accessKeyId":"ak_info","secretAccessKey":"sk_info"} - Set job parameters on the AI DataLake Flink JAR job editing page.
- Class name (The following is an example. Replace it with the actual fully qualified class name of the main class in your JAR package.):
1com.example.dew.DataGen2FileSystemSink
- Parameter:
1--output.path obs://USER_BUCKET_NAME/output/DataGen2FileSystemSink - Optimization parameters:
1 2 3 4 5 6
flink.hadoop.fs.obs.bucket.USER_BUCKET_NAME.dew.access.key=USER_AK_CSMS_KEY flink.hadoop.fs.obs.bucket.USER_BUCKET_NAME.dew.secret.key=USER_SK_CSMS_KEY flink.hadoop.fs.obs.security.provider=com.aidatalake.flink.auth.provider.DewObsCredentialProvider flink.hadoop.fs.dew.csms.secretName=obsAksK flink.hadoop.fs.dew.csms.version=v1 flink.hadoop.fs.dew.csms.cache.time.second=3600
- Class name (The following is an example. Replace it with the actual fully qualified class name of the main class in your JAR package.):
- Flink JAR job example
- Environment preparation
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 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<properties> <flink.version>1.20.0</flink.version> </properties> <dependencies> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-statebackend-rocksdb</artifactId> <version>${flink.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-streaming-java</artifactId> <version>${flink.version}</version> <scope>provided</scope> </dependency> <!-- fastjson --> <dependency> <artifactId>fastjson</artifactId> <version>2.0.15</version> </dependency> </dependencies>
- Sample code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
package com.example.dew; import org.apache.flink.api.common.serialization.SimpleStringEncoder; import org.apache.flink.api.java.utils.ParameterTool; import org.apache.flink.contrib.streaming.state.EmbeddedRocksDBStateBackend; import org.apache.flink.core.fs.Path; import org.apache.flink.streaming.api.datastream.DataStream; import org.apache.flink.streaming.api.environment.CheckpointConfig; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink; import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.OnCheckpointRollingPolicy; import org.apache.flink.streaming.api.functions.source.ParallelSourceFunction; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Random; public class DataGen2FileSystemSink { private static final Logger LOG = LoggerFactory.getLogger(DataGen2FileSystemSink.class); public static void main(String[] args) { ParameterTool params = ParameterTool.fromArgs(args); LOG.info("Params: " + params.toString()); try { StreamExecutionEnvironment streamEnv = StreamExecutionEnvironment.getExecutionEnvironment(); // The checkpoint storage directory is automatically injected by the platform (execution.checkpointing.dir is a reserved parameter in the system) and does not need to be customized. streamEnv.setStateBackend(new EmbeddedRocksDBStateBackend()); streamEnv.getCheckpointConfig().setExternalizedCheckpointCleanup( CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION); streamEnv.enableCheckpointing(30 * 1000); DataStream<String> stream = streamEnv.addSource(new DataGen()) .setParallelism(1) .disableChaining(); String outputPath = params.get("output.path", "obs://bucket/outputPath/jobId_jobName"); // Sink OBS final StreamingFileSink<String> sinkForRow = StreamingFileSink .forRowFormat(new Path(outputPath), new SimpleStringEncoder<String>("UTF-8")) .withRollingPolicy(OnCheckpointRollingPolicy.build()) .build(); stream.addSink(sinkForRow); streamEnv.execute("sinkForRow"); } catch (Exception e) { LOG.error(e.getMessage(), e); } } } class DataGen implements ParallelSourceFunction<String> { private boolean isRunning = true; private Random random = new Random(); @Override public void run(SourceContext<String> ctx) throws Exception { while (isRunning) { JSONObject jsonObject = new JSONObject(); jsonObject.put("id", random.nextLong()); jsonObject.put("name", "Molly" + random.nextInt()); jsonObject.put("address", "hangzhou" + random.nextInt()); jsonObject.put("birthday", System.currentTimeMillis()); jsonObject.put("city", "hangzhou" + random.nextInt()); jsonObject.put("number", random.nextInt()); ctx.collect(jsonObject.toJSONString()); Thread.sleep(1000); } } @Override public void cancel() { isRunning = false; } }
- Environment preparation
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot