// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.jdisc.cloud.aws;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.PropertiesCredentials;

import java.nio.file.Path;
import java.util.concurrent.atomic.AtomicReference;

public class VespaAwsCredentialsProvider implements AWSCredentialsProvider {

    private static final String DEFAULT_CREDENTIALS_PATH = "/opt/vespa/var/container-data/opt/vespa/conf/credentials.properties";

    private final AtomicReference<AWSCredentials> credentials = new AtomicReference<>();
    private final Path credentialsPath;

    public VespaAwsCredentialsProvider() {
        this.credentialsPath = Path.of(DEFAULT_CREDENTIALS_PATH);
        refresh();
    }

    @Override
    public AWSCredentials getCredentials() {
        return credentials.get();
    }

    @Override
    public void refresh() {
        try {
            // TODO : implement reading from json file
            PropertiesCredentials propertiesCredentials = new PropertiesCredentials(this.credentialsPath.toFile());
            credentials.set(propertiesCredentials);
        } catch (Exception e) {
            throw new RuntimeException("Unable to get credentials in " + credentialsPath.toString(), e);
        }
    }
}

@@ -265,31 +265,234 @@
 uth.
-PropertiesCredentials;%0A
+AWSSessionCredentials;%0Aimport com.amazonaws.auth.BasicSessionCredentials;%0Aimport com.yahoo.slime.Cursor;%0Aimport com.yahoo.slime.Slime;%0Aimport com.yahoo.slime.SlimeUtils;%0A%0Aimport java.io.IOException;%0Aimport java.nio.file.Files;
 %0Aimp
@@ -751,16 +751,22 @@
 pa/conf/
+vespa/
 credenti
@@ -769,26 +769,20 @@
 entials.
-properties
+json
 %22;%0A%0A    
@@ -1214,50 +1214,180 @@
     
-// TODO : implemen
+credentials.set(readCredentials());%0A        %7D catch (Exception e) %7B%0A            throw new RuntimeException(%22Unable to ge
 t 
+c
 re
-ading from json file
+dentials in %22 + credentialsPath.toString(), e);
 %0A   
@@ -1395,22 +1395,39 @@
     
+%7D%0A
     
-Properties
+%7D%0A%0A    private AWSSession
 Cred
@@ -1434,26 +1434,20 @@
 entials 
-properties
+read
 Credenti
@@ -1453,118 +1453,455 @@
 ials
- = new PropertiesCreden
+() %7B%0A        try %7B%0A            Slime slime = SlimeU
 ti
-a
 ls
-(this.credentialsPath.toFile());%0A            credentials.set(propertiesCredentials
+.jsonToSlime(Files.readAllBytes(credentialsPath));%0A            Cursor cursor = slime.get();%0A            String accessKey = cursor.field(%22awsAccessKey%22).asString();%0A            String secretKey = cursor.field(%22awsSecretKey%22).asString();%0A            String sessionToken = cursor.field(%22sessionToken%22).asString();%0A            return new BasicSessionCredentials(accessKey, secretKey, sessionToken
 );%0A 
@@ -1908,32 +1908,34 @@
        %7D catch (
+IO
 Exception e) %7B%0A 
@@ -1976,70 +1976,8 @@
 ion(
-%22Unable to get credentials in %22 + credentialsPath.toString(), 
 e);%0A

Read IAM session credentials from file