Notice
Recent Posts
Recent Comments
Link
투케이2K
977. (Android/Java) [간단 소스] Aws STS 임시 자격 증명 요청 및 AccessKeyId, SecretAccessKey, SessionToken 확인 본문
Android
977. (Android/Java) [간단 소스] Aws STS 임시 자격 증명 요청 및 AccessKeyId, SecretAccessKey, SessionToken 확인
투케이2K 2025. 4. 27. 10:30728x90
[개발 환경 설정]
개발 툴 : AndroidStudio
개발 언어 : Java / Kotlin

[소스 코드]
// --------------------------------------------------------------------------------------
[개발 및 테스트 환경]
// --------------------------------------------------------------------------------------
- 언어 : Java / Kotlin
- 개발 툴 : AndroidStudio
- 기술 구분 : Aws / STS / 임시 자격 증명
// --------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------
[사전) 필요 설정 정리] : build.gradle 의존성 부여
// --------------------------------------------------------------------------------------
// [AWS] : [target 31 이상 의존성]
implementation 'com.amazonaws:aws-android-sdk-kms:2.57.0'
implementation 'com.amazonaws:aws-android-sdk-s3:2.57.0'
implementation 'com.amazonaws:aws-android-sdk-iot:2.57.0'
implementation 'com.amazonaws:aws-android-sdk-mobile-client:2.57.0'
// [AWS] : [target 31 미만 의존성]
implementation 'com.amazonaws:aws-android-sdk-kms:2.16.13'
implementation 'com.amazonaws:aws-android-sdk-s3:2.16.13'
implementation 'com.amazonaws:aws-android-sdk-iot:2.16.13'
implementation 'com.amazonaws:aws-android-sdk-mobile-client:2.16.13'
// --------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------
[Java : 소스 코드]
// --------------------------------------------------------------------------------------
// -------------------------------------------------
// [변수 선언]
// -------------------------------------------------
String ACCESS_KEY = "AK...A6";
String SECRET_KEY = "mP...5J";
String ROLE_ARN = "arn:aws: ...";
private static AWSSecurityTokenServiceClient awsSecurityTokenServiceClient = null; // TODO [AWSSecurityTokenServiceClient 객체]
private static AssumeRoleRequest assumeRoleRequest = null; // TODO [AssumeRoleRequest 객체]
// -------------------------------------------------
// TODO [awsCredentialsInit 초기화 수행]
// -------------------------------------------------
public boolean awsCredentialsInit(Context ctx, String accessKey, String secretKey, String roleArn) {
S_Log._D_(ACTIVITY_NAME + " :: setContext :: AWSCredentials 초기화 수행", new String[]{"accessKey :: " + String.valueOf(accessKey), "secretKey :: " + String.valueOf(secretKey), "roleArn :: " + String.valueOf(roleArn)});
boolean returnData = false;
try {
mMainCtx = ctx;
if (C_Util.stringNotNull(accessKey) == true
&& C_Util.stringNotNull(secretKey) == true
&& C_Util.stringNotNull(roleArn) == true){ // [파라미터 null 체크 수행]
// [AWSCredentials 정의]
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
// [AWSSecurityTokenServiceClient 정의]
awsSecurityTokenServiceClient = new AWSSecurityTokenServiceClient(credentials);
// [세션 명칭 정의]
String sessionName = C_Util.getFormNowDate("yyyMMddHHmmss") + "_" + "STS_Session";
// [AWSKMSClient 초기화]
assumeRoleRequest = new AssumeRoleRequest();
assumeRoleRequest.setRequestCredentials(credentials);
assumeRoleRequest.withRoleArn(roleArn);
assumeRoleRequest.withRoleSessionName(sessionName);
assumeRoleRequest.withDurationSeconds(3600); // 유효 시간 (1시간)
S_Log._W_(ACTIVITY_NAME + " :: setContext :: AWSCredentials 초기화 성공", new String[]{sessionName});
returnData = true;
}
else {
S_Log._E_(ACTIVITY_NAME + " :: setContext :: AWSCredentials 초기화 에러", new String[]{"Error :: Input Data Is Null"});
}
}
catch (Exception e){
S_Log._printStackTrace_(null, S_FinalData.LOG_BUG_STATE, null, e);
}
return returnData;
}
// -------------------------------------------------
// TODO [AWS STS 임시 자격 증명 확인 수행]
// -------------------------------------------------
public Observable<HashMap<String, String>> getAwsSts() {
S_Log._D_(ACTIVITY_NAME + " :: getAwsSts :: AWS STS 임시 자격 증명 확인 수행", null);
return Observable.create(subscriber -> {
new Thread(() -> {
try {
if (awsSecurityTokenServiceClient != null && assumeRoleRequest != null){
// TODO [STS 임시 자격 증명 확인 요청]
AssumeRoleResult assumeRoleResult = awsSecurityTokenServiceClient.assumeRole(assumeRoleRequest);
Credentials tempCredentials = assumeRoleResult.getCredentials();
// TODO [결과 확인]
String tempAccessKeyId = tempCredentials.getAccessKeyId();
String tempSecretAccessKey = tempCredentials.getSecretAccessKey();
String sessionToken = tempCredentials.getSessionToken();
// TODO [리턴 변수 삽입]
HashMap<String, String> returnData = new HashMap<String, String>();
returnData.put("ACCESS_KEY_ID", String.valueOf(tempAccessKeyId));
returnData.put("SECRET_ACCESS_KEY", String.valueOf(tempSecretAccessKey));
returnData.put("SESSION_TOKEN", String.valueOf(sessionToken));
S_Log._W_(ACTIVITY_NAME + " :: getAwsSts :: AWS STS 임시 자격 증명 확인 성공", new String[]{String.valueOf(returnData)});
if (subscriber != null && subscriber.isDisposed() == false){
subscriber.onNext(returnData);
subscriber.onComplete();
}
}
else {
S_Log._E_(ACTIVITY_NAME + " :: getAwsSts :: AWS STS 임시 자격 증명 확인 에러", new String[]{"Error :: awsSecurityTokenServiceClient is null"});
if (subscriber != null && subscriber.isDisposed() == false){
subscriber.onError(new Throwable("[Error] : awsSecurityTokenServiceClient is null"));
subscriber.onComplete();
}
}
} catch (final Exception e){
S_Log._printStackTrace_(null, S_FinalData.LOG_BUG_STATE, null, e);
try {
if (subscriber != null && subscriber.isDisposed() == false){
subscriber.onError(new Throwable("[Exception] : " + String.valueOf(e.getMessage())));
subscriber.onComplete();
}
}
catch (Exception ex){
ex.printStackTrace();
}
}
}).start();
});
}
// --------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------
[참고 사이트]
// --------------------------------------------------------------------------------------
[Aws Security Token Service] Aws STS 임시 보안 자격 증명 설명 정리
https://blog.naver.com/kkh0977/223846461194?trackingCode=blog_bloghome_searchlist
// --------------------------------------------------------------------------------------
728x90
반응형
'Android' 카테고리의 다른 글
Comments