Skip to content

Commit

Permalink
Merge pull request #1 from adRise/huajiang/build-client-with-proper-r…
Browse files Browse the repository at this point in the history
…egion

build codeartifact client with the proper aws region
  • Loading branch information
huajiang-tubi authored Nov 11, 2024
2 parents 87713d2 + 5a95fd0 commit 118f862
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
9 changes: 6 additions & 3 deletions core/src/main/scala/codeartifact/CodeArtifact.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package codeartifact

import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.codeartifact.CodeartifactClient
import software.amazon.awssdk.services.codeartifact.model.GetAuthorizationTokenRequest

Expand All @@ -23,15 +24,17 @@ object CodeArtifact {
.durationSeconds(15.minutes.toSeconds)
.build()

private def getAuthTokenFromRequest(req: GetAuthorizationTokenRequest): String =
private def getAuthTokenFromRequest(region: Region, req: GetAuthorizationTokenRequest): String =
CodeartifactClient
.create()
.builder()
.region(region)
.build()
.getAuthorizationToken(req)
.authorizationToken()

def getAuthToken(repo: CodeArtifactRepo): Option[String] =
try {
Some(getAuthTokenFromRequest(getAuthorizationTokenRequest(repo.domain, repo.owner)))
Some(getAuthTokenFromRequest(Region.of(repo.region), getAuthorizationTokenRequest(repo.domain, repo.owner)))
} catch {
case _: Throwable => None
}
Expand Down
33 changes: 7 additions & 26 deletions core/src/main/scala/codeartifact/CodeArtifactRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,22 @@ case class CodeArtifactRepo(
domain: String,
host: String,
owner: String,
region: String,
url: String
) {
def realm: String = s"$domain/$name"
def resolver: MavenRepository = realm.at(url)
}

object CodeArtifactRepo {
private val CodeArtifactUrl = "https://((.*)-(.*).d.codeartifact.(.*).amazonaws.com)/maven/(.*)".r

def fromUrl(url: String): CodeArtifactRepo = {
if (url.isEmpty()) {
sys.error("""codeArtifactUrl not defined. assign this with codeArtifactUrl := "your-url"""")
url match {
case CodeArtifactUrl(host, domain, owner, region, repo) =>
CodeArtifactRepo(name = repo, domain = domain, host = host, owner = owner, region = region, url = url)
case _ =>
sys.error(s"Invalid codeArtifactUrl: $url")
}

// Url looks like:
// https://<domain>-<owner>.d.codeartifact.us-west-2.amazonaws.com/maven/<repository>
val juri = new java.net.URI(url)

// Split on slashes, and get the last element: <repository>.
val name = juri.getPath().split('/').last

// Split on dots. Take the head, which is the <domain>-<owner> section.
// Split on dashes.
val host = juri.getHost()
val parts = host.split('.').head.split('-')
// Last element is <owner>.
val owner = parts.last
// The rest are the <domain>. Merge them together again.
val domain = parts.init.mkString("-")

CodeArtifactRepo(
name = name,
domain = domain,
host = host,
owner = owner,
url = url
)
}
}

0 comments on commit 118f862

Please sign in to comment.