Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build codeartifact client with the proper aws region #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)
}
}