No, I’m not asking you write my code for me
Yes you do, even if it is only for a PoC.
I gave you exact instructions that you just would need to follow.
because the interaction so far has been unfruitful. For both of us.
Well, if you would have followed my instructions, it would have been more fruiful.
I know quite well how you feel and why it won’t work as long as we keep interacting this way.
Well, if you would just have changed then to doing what I said, it would have worked.
Do you think a pull request is ineffective?
Not at all.
But giving you the information you need to do it yourself instead has multiple advantages. For example
- you learn more if you manage to do it yourself instead of being fed
- it costs me some minutes to give you the instructions that you need to do it yourself
- it costs me significantly more effort and time to write the code for you and I have plenty other stuff to do, including my day-job
- others can benefit from the information here too, while the PoC will most probably be deleted and others cannot see the changes
Having said all that, lucky you I had some free minutes.
I’ll attach it as patch here instead of a PR because of the last point above.
You probably will need to polish it some more like adding a ReentrantReadWriteLock
for accessing the token and adding the expiry check.
And it of course is greatly untested, as I neither have an AWS account, not aws
utility.
diff --git a/buildSrc/src/main/groovy/contoso/ContosoAwsCredentialsService.groovy b/buildSrc/src/main/groovy/contoso/ContosoAwsCredentialsService.groovy
new file mode 100644
index 0000000..234e7cd
--- /dev/null
+++ b/buildSrc/src/main/groovy/contoso/ContosoAwsCredentialsService.groovy
@@ -0,0 +1,38 @@
+package contoso
+
+import org.gradle.api.provider.Property
+import org.gradle.api.services.BuildService
+import org.gradle.api.services.BuildServiceParameters
+import org.gradle.process.ExecOperations
+
+import javax.inject.Inject
+
+abstract class ContosoAwsCredentialsService implements BuildService<Parameters> {
+ @Inject
+ abstract ExecOperations getExec()
+ private String cachedToken
+
+ String getAccessToken() {
+ if ((cachedToken == null) || cachedAccessTokenExpired) {
+ cachedToken = freshAccessTokenWithAwsSdk
+ }
+ return cachedToken
+ }
+
+ private boolean isCachedAccessTokenExpired() {
+ return false
+ }
+
+ private String getFreshAccessTokenWithAwsSdk() {
+ ByteArrayOutputStream stdout = new ByteArrayOutputStream()
+ exec.exec {
+ it.commandLine('aws', 'codeartifact', 'get-authorization-token', '--domain', 'temporary', '--query', 'authorizationToken', '--output', 'text', '--profile', parameters.awsProfile)
+ it.standardOutput = stdout
+ }
+ return stdout.toString()
+ }
+
+ interface Parameters extends BuildServiceParameters {
+ Property<String> getAwsProfile()
+ }
+}
diff --git a/buildSrc/src/main/groovy/contoso/ContosoGradlePlugin.groovy b/buildSrc/src/main/groovy/contoso/ContosoGradlePlugin.groovy
index bcef72d..8c3cf1b 100644
--- a/buildSrc/src/main/groovy/contoso/ContosoGradlePlugin.groovy
+++ b/buildSrc/src/main/groovy/contoso/ContosoGradlePlugin.groovy
@@ -1,11 +1,25 @@
package contoso;
import org.gradle.api.Plugin;
-import org.gradle.api.Project;
+import org.gradle.api.Project
+import org.gradle.api.services.BuildServiceRegistry
+
+import javax.inject.Inject;
+
+abstract class ContosoGradlePlugin implements Plugin<Project> {
+ @Inject
+ abstract BuildServiceRegistry getSharedServices()
-class ContosoGradlePlugin implements Plugin<Project> {
@Override
void apply(Project project) {
- project.getExtensions().create("contoso", ContosoMavenExtension)
+ def contosoAwsCredentials = sharedServices.registerIfAbsent("contosoAwsCredentials", ContosoAwsCredentialsService) {
+ parameters {
+ awsProfile = project.property('contoso.aws.profile')
+ }
+ }
+ project.repositories.extensions.create("contoso", ContosoMavenExtension, project.repositories, project, contosoAwsCredentials)
+ project.pluginManager.withPlugin("maven-publish") {
+ project.publishing.repositories.extensions.create("contoso", ContosoMavenExtension, project.publishing.repositories, project, contosoAwsCredentials)
+ }
}
-}
\ No newline at end of file
+}
diff --git a/buildSrc/src/main/groovy/contoso/ContosoMavenExtension.groovy b/buildSrc/src/main/groovy/contoso/ContosoMavenExtension.groovy
index 56e4a2d..0a5b170 100644
--- a/buildSrc/src/main/groovy/contoso/ContosoMavenExtension.groovy
+++ b/buildSrc/src/main/groovy/contoso/ContosoMavenExtension.groovy
@@ -2,25 +2,28 @@ package contoso
import org.gradle.api.Project
import org.gradle.api.artifacts.dsl.RepositoryHandler
+import org.gradle.api.provider.Provider
abstract class ContosoMavenExtension {
private final RepositoryHandler repositories
private final Project project
+ private final Provider<ContosoAwsCredentialsService> contosoAwsCredentials
- ContosoMavenExtension(RepositoryHandler repositories, Project project) {
+ ContosoMavenExtension(RepositoryHandler repositories, Project project, Provider<ContosoAwsCredentialsService> contosoAwsCredentials) {
this.repositories = repositories
this.project = project
+ this.contosoAwsCredentials = contosoAwsCredentials
}
def codeArtifact() {
- def endpoint = project.property("contoso.m2.endpoint")
- def awsProfile = project.property('contoso.aws.profile')
+ def contosoAwsCredentials = contosoAwsCredentials
+ def endpoint = project.property('contoso.m2.endpoint')
repositories.maven {
url endpoint
credentials {
username = 'aws'
- password = "aws codeartifact get-authorization-token --domain temporary --query authorizationToken --output text --profile ${awsProfile}".execute().getText()
+ password = contosoAwsCredentials.get().accessToken
}
}
}