amazon-web-servicesdockermavenaws-codeartifact

maven download jar from AWS codeartifact inside docker container build


I have an issue, I am attempting to build a docker image for building and running tests on my project. I use AWS codeartifact to provide my jar files and am having issues getting it to work. I have provided the id and secret via environment variables and tested everything locally which works fine. The docker image downloads and installs the AWS CLI interface I configure the credentials but it still gives me a 401. I am just wondering if someone else has tried something similar and could spot what I have done incorrectly.

Dockerfile

FROM --platform=linux/amd64 amazoncorretto:21.0.2

ARG MAVEN_VERSION=3.9.6

# 2- Define a constant with the working directory
ARG USER_HOME_DIR="/root"

ARG AWS_CLI_LINK=https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip

ARG AWS_ACCESS_KEY_ID
ARG AWS_SECRET_ACCESS_KEY
ARG CODEARTIFACT_AUTH_TOKEN

RUN echo $AWS_ACCESS_KEY_ID
RUN echo $AWS_SECRET_ACCESS_KEY
RUN echo $CODEARTIFACT_AUTH_TOKEN
#RUN mkdir -p .m2
#COPY settings-amazon-lawyrup.xml .m2/settings.xml


RUN mkdir -p /usr/share/aws
RUN echo "Downloading aws cli"
RUN curl -fsSL -o /tmp/awscli-exe-linux-x86_64.zip ${AWS_CLI_LINK}
RUN unzip /tmp/awscli-exe-linux-x86_64.zip -d /usr/share/aws
RUN /usr/share/aws/aws/install
RUN aws --version
RUN aws configure set aws_access_key_id $$AWS_ACCESS_KEY
RUN aws configure set aws_secret_access_key $$AWS_SECRET_KEY

#maven download & install
RUN mkdir -p /usr/share/maven /usr/share/maven/ref
RUN echo "Downloading maven"
RUN curl -fsSL -o /tmp/apache-maven.tar.gz ${BASE_URL}/apache-maven-${MAVEN_VERSION}-bin.tar.gz
RUN echo "Checking download hash"
RUN echo "${MAVAN_SUM}  /tmp/apache-maven.tar.gz" | sha512sum -c -

RUN echo "Unziping maven"
RUN tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1

RUN echo "Cleaning and setting links"
RUN rm -f /tmp/apache-maven.tar.gz
RUN ln -s /usr/share/maven/bin/mvn /usr/bin/mvn

# 6- Define environmental variables required by Maven, like Maven_Home directory and where the maven repo is located
ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2"

# Cache Maven Dependencies
COPY pom.xml ./pom.xml
RUN mvn -f pom.xml -B dependency:resolve-plugins dependency: resolve

The error is

artifacts could not be resolved: com.lawyrup:immutabletypes:pom:0.1.0.3 (absent): Could not transfer artifact com.lawyrup:immutabletypes:pom:0.1.0.3 from/to lawyrup-lawyrup (https://lawyrup-437075886573.d.codeartifact.ap-southeast-2.amazonaws.com/maven/lawyrup/): status code: 401, reason phrase: Unauthorized (401) -> [Help 1]


Solution

  • The artifact mentioned in the error message can not be found in common public Maven repositories. Therefore I assume it is located in a private Maven repository that requires special access credentials.

    user specific access credentials are configured in Maven's settings files which is located here: ~/.m2/settings.xml

    Check the pom.xml for the defined repository and it's <id>. For this id you need to specify access credentials in settings.xml.

    An example that uses username/password as access credential is shown e.g. in this Stackoverflow question: Authorization Header not being sent to Maven Repository

    An example for using a private token can be found in this question: sbt: Add http headers when fetching private maven repo with credentials