/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.fs.s3native.token;

import org.apache.flink.annotation.Internal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
import software.amazon.awssdk.services.sts.model.Credentials;

@Internal
public class DynamicTemporaryAWSCredentialsProvider implements AwsCredentialsProvider {

    private static final Logger LOG =
            LoggerFactory.getLogger(DynamicTemporaryAWSCredentialsProvider.class);

    @Override
    public AwsCredentials resolveCredentials() {
        Credentials credentials = NativeS3DelegationTokenReceiver.getCredentials();
        if (credentials == null) {
            // AWS SDK chain expects an exception when provider doesn't have credentials.
            // This signals the chain to try the next provider in the chain.
            LOG.debug(
                    "No delegation token credentials available - "
                            + "AWS SDK will try next provider in chain");
            throw new CredentialsNotAvailableException(
                    "No delegation token credentials available - proceeding to next provider in chain");
        }
        LOG.debug(
                "Using delegation token credentials with access key: {}",
                credentials.accessKeyId());
        return AwsSessionCredentials.create(
                credentials.accessKeyId(),
                credentials.secretAccessKey(),
                credentials.sessionToken());
    }

    /**
     * Exception thrown when delegation token credentials are not available. This is caught by the
     * AwsCredentialsProviderChain to proceed to the next provider.
     */
    public static class CredentialsNotAvailableException extends RuntimeException {
        public CredentialsNotAvailableException(String message) {
            super(message);
        }
    }
}
