LoginSignup
1
0

More than 3 years have passed since last update.

GradleでCodeArtifactの認証をプロファイル由来の自動化

Posted at

はじめに

レポジトリがマネージドになってすこぶる便利になりそうです。
が、公式ドキュメントを読むと環境変数にトークンをロードしておけと、、、
毎回そんなめんどくさいことやれと?やりたくない!

というわけでAWS CLIで設定されてるであろうプロファイルから読むように書いてみました。

前提

任意のCodeArtifactにアクセスできるAWS CLIのプロファイルが設定済みのこと

使い方

以下を丸ごとコピー
※直接ファイルで落としたいときはこっち

build.gradle
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.amazonaws:aws-java-sdk-codeartifact:1.11.801'
    }
}

import com.amazonaws.services.codeartifact.AWSCodeArtifactClient;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.codeartifact.model.GetAuthorizationTokenRequest;
def setAuthorizationToken = {mavenArtifactRepository, profile ->
    def domainLevels = mavenArtifactRepository.url.getHost().split('\\.')
    def artifactDomain = domainLevels[0].substring(0,domainLevels[0].lastIndexOf("-"))
    def artifactOwner = domainLevels[0].substring(domainLevels[0].lastIndexOf("-")+1)
    def region = domainLevels[domainLevels.length -3]

    def client = AWSCodeArtifactClient.builder()
        .withCredentials(new ProfileCredentialsProvider(profile))
        .withRegion(region)
    .build();

    def result = client.getAuthorizationToken(new GetAuthorizationTokenRequest()
        .withDomain(artifactDomain)
        .withDomainOwner(artifactOwner)
    );

    mavenArtifactRepository.credentials {
        username "aws"
        password result.authorizationToken
    }
}


apply plugin: 'java'
apply plugin: 'eclipse'

repositories {
    maven {
        url 'https://trial-558497472117.d.codeartifact.us-west-2.amazonaws.com/maven/trial/'
        setAuthorizationToken(owner, "profileName")
    }
}

dependencies {
    implementation platform('com.amazonaws:aws-java-sdk-bom:1.11.801')
    implementation 'com.amazonaws:aws-java-sdk-codeartifact'
}


以下のここをプロファイル名に置き換えればOK

setAuthorizationToken(owner, "ここ")//←プロファイル名

参考先

余談

s3だったらgradleのwrapperの中に入ってる(lib\plugins\aws-java-sdk-s3-1.11.xxx.jar)からbuildscriptで引いてこなくていいんだけどなぁ、、、
buildscriptのaws-java-sdk-codeartifactが最初引けないから完全CodeArtifactのみのスタンドアローンにはできない。ローカルにキャッシュした後ならいけるんだけども、、、
AWS 管理ポリシーにAWSCodeArtifactReadOnlyAccess的なやつはまだなかった。(将来できる?
CodeArtifactだけじゃ足りなくて、sts:GetServiceBearerTokenも必要なので注意を!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0