All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.hadoop.fs.auth.CVMInstanceCredentialsProvider Maven / Gradle / Ivy

Go to download

This module contains code to support integration with Tencent Cloud COS. It also declares the dependencies needed to work with COS.

There is a newer version: 8.2.7
Show newest version
package org.apache.hadoop.fs.auth;

import com.qcloud.cos.auth.*;
import com.qcloud.cos.exception.CosClientException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.CosNConfigKeys;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.net.URI;

/**
 * Provide the credentials when the CosN connector is instantiated on Tencent Cloud Virtual Machine(CVM)
 */
public class CVMInstanceCredentialsProvider extends AbstractCOSCredentialProvider implements COSCredentialsProvider {
    private static final Logger LOG = LoggerFactory.getLogger(CVMInstanceCredentialsProvider.class);

    private String appId;
    private final COSCredentialsProvider cosCredentialsProvider;

    public CVMInstanceCredentialsProvider(@Nullable URI uri, Configuration conf) {
        super(uri, conf);
        if (null != conf) {
            this.appId = conf.get(CosNConfigKeys.COSN_APPID_KEY);
        }
        InstanceMetadataCredentialsEndpointProvider endpointProvider =
                new InstanceMetadataCredentialsEndpointProvider(
                        InstanceMetadataCredentialsEndpointProvider.Instance.CVM);
        InstanceCredentialsFetcher instanceCredentialsFetcher = new InstanceCredentialsFetcher(endpointProvider);
        this.cosCredentialsProvider = new InstanceCredentialsProvider(instanceCredentialsFetcher);
    }

    @Override
    public COSCredentials getCredentials() {
        try {
            COSCredentials cosCredentials = this.cosCredentialsProvider.getCredentials();
            // Compatible appId
            if (null != this.appId) {
                if (cosCredentials instanceof InstanceProfileCredentials) {
                    return new InstanceProfileCredentials(this.appId, cosCredentials.getCOSAccessKeyId(),
                            cosCredentials.getCOSSecretKey(),
                            ((InstanceProfileCredentials) cosCredentials).getSessionToken(),
                            ((InstanceProfileCredentials) cosCredentials).getExpiredTime());
                }
            }
            return cosCredentials;
        } catch (CosClientException e) {
            LOG.error("Failed to obtain the credentials from CVMInstanceCredentialsProvider.", e);
        }

        return null;
    }

    @Override
    public void refresh() {
        this.cosCredentialsProvider.refresh();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy