Skip to content

Add Docker login to Jenkins script to avoid rate limit issue #6368

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

Merged
merged 2 commits into from
May 30, 2024

Conversation

XinShuYang
Copy link
Contributor

Enhance Jenkins pipeline by integrating Docker credentials for login to avoid rate limit issues

@XinShuYang
Copy link
Contributor Author

/test-e2e

1 similar comment
@XinShuYang
Copy link
Contributor Author

/test-e2e

@@ -984,6 +994,7 @@ if [[ $TESTCASE =~ "multicast" ]]; then
fi

source $WORKSPACE/ci/jenkins/utils.sh
docker_login "${dockerUser}" "${dockerPassword}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there is such convention: should it disable xtrace first before using the secret, otherwise it would be printed to terminal by enabling xtrace in other place in this script and it won't be easy to notice.
The following code should ensure it won't be printed:

(
  set +x 
  docker_login "${dockerUser}" "${dockerPassword}"
)

@antoninbas do you know how is this usually handled?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The secret is stored in Jenkins credentials and will not be exposed in the results. However, we should indeed avoid printing too much information about the login step, will update the code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I think like this should not even be in the script if possible.
Isn't there a way to configure Jenkins so that we login to Docker before even calling this script?

Copy link
Contributor Author

@XinShuYang XinShuYang May 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antoninbas Do you mean to put this login script into Jenkins macros to avoid modifying the test.sh related scripts? Using this method, we would need to modify the macros definition for all related jobs. In the Nephe CI pipeline, we have verified a similar method to log into Docker as we did in this PR. https://github.com/antrea-io/nephe/blob/9faeba2c189821045e4ea6d37a1c97dbdecf67bd/ci/jenkins/nephe-ci.sh#L112

And I haven't found another way to log into Docker before running script in a freestyle job. It is possible to configure Docker login through a Jenkins pipeline job, but pipeline jobs are not compatible with the freestyle Jenkins setup we are currently using.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I think like this should not even be in the script if possible. Isn't there a way to configure Jenkins so that we login to Docker before even calling this script?

Yes, Handling Docker login credentials within a script can be a security risk. It's generally better to manage authentication outside of the script . isn't the docker credentials be stored in jenkins credentials and used to access it using credentials in job itself, In that case it has be configured in each and every job then !

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I think like this should not even be in the script if possible. Isn't there a way to configure Jenkins so that we login to Docker before even calling this script?

Yes, Handling Docker login credentials within a script can be a security risk. It's generally better to manage authentication outside of the script . isn't the docker credentials be stored in jenkins credentials and used to access it using credentials in job itself, In that case it has be configured in each and every job then !

When using the Jenkins Credential secrets, you can securely inject sensitive credentials into the build process as environment variables. These variables are visible during use, but Jenkins automatically masks their values to prevent them from being displayed in the build logs.

After consideration, I placed the login script in the macro YAML, which ensures that the login step cannot be changed by any PR.

@XinShuYang
Copy link
Contributor Author

/test-all

@XinShuYang XinShuYang marked this pull request as ready for review May 24, 2024 09:32
@XinShuYang
Copy link
Contributor Author

/test-all

@XinShuYang
Copy link
Contributor Author

/test-conformance

@@ -103,7 +103,7 @@
#!/bin/bash
set -ex
DOCKER_REGISTRY="$(head -n1 ci/docker-registry)"
./ci/jenkins/test-vmc.sh --cluster-name "$BUILD_TAG" --testcase e2e --coverage --codecov-token "${CODECOV_TOKEN}" --registry ${DOCKER_REGISTRY} --username "${CAPVC_USERNAME}" --password "${CAPVC_PASSWORD}"
./ci/jenkins/test-vmc.sh --cluster-name "$BUILD_TAG" --testcase e2e --coverage --codecov-token "${CODECOV_TOKEN}" --registry ${DOCKER_REGISTRY} --username "${CAPVC_USERNAME}" --password "${CAPVC_PASSWORD}" --dockerUser "${{DOCKER_USERNAME}}" --dockerPassword "${{DOCKER_PASSWORD}}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better to follow the same name pattern as others, e.g. '--docker-user' --docker-password

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, updated.

@XinShuYang XinShuYang force-pushed the dockerlogin branch 5 times, most recently from cc79745 to cbad3f4 Compare May 27, 2024 10:21
@XinShuYang
Copy link
Contributor Author

/test-all

@XinShuYang
Copy link
Contributor Author

/test-conformance

./ci/jenkins/docker_login.sh --docker-user "${{DOCKER_USERNAME}}" --docker-password "${{DOCKER_PASSWORD}}"
DOCKER_REGISTRY="$(head -n1 ci/docker-registry)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a little bit strange that we call docker_login.sh unconditionally, and then we run DOCKER_REGISTRY. In theory, if we have DOCKER_REGISTRY="$(head -n1 ci/docker-registry)", that means that DOCKER_REGISTRY is not guaranteed to be docker.io, but docker_login.sh assumes that the registry is docker.io.

Maybe these 2 instructions should be swapped, and docker_login.sh should return immediately if DOCKER_REGISTRY is not docker.io? Or some other solution that makes docker login conditional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it make sense, I add a registry flag for the login script.

@XinShuYang XinShuYang force-pushed the dockerlogin branch 2 times, most recently from 429f614 to 98e81de Compare May 29, 2024 02:56
@XinShuYang
Copy link
Contributor Author

/test-all

@XinShuYang
Copy link
Contributor Author

/test-all

echo "Logging into Docker Hub..."
docker_login "${DOCKER_USERNAME}" "${DOCKER_PASSWORD}"
else
echo "Registry $DOCKER_REGISTRY is not supported for login by this script."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it exit with non-zero?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it feels to me that we should just avoid calling the script in this case.
If we use the following, it won't really be more verbose than what we have currently:

[ "$DOCKER_REGISTRY" == "docker.io" ] || ./ci/jenkins/docker_login.sh --docker-user ${{DOCKER_USERNAME}} --docker-password ${{DOCKER_PASSWORD}}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it feels to me that we should just avoid calling the script in this case. If we use the following, it won't really be more verbose than what we have currently:

[ "$DOCKER_REGISTRY" == "docker.io" ] || ./ci/jenkins/docker_login.sh --docker-user ${{DOCKER_USERNAME}} --docker-password ${{DOCKER_PASSWORD}}

Got the point, updated the macro script, PTAL.

Copy link
Contributor Author

@XinShuYang XinShuYang May 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antoninbas I think it should be
[ "$DOCKER_REGISTRY" != "docker.io" ] || ./ci/jenkins/docker_login.sh --docker-user ${{DOCKER_USERNAME}} --docker-password ${{DOCKER_PASSWORD}} right?
The docker_login.sh should only be executed when $DOCKER_REGISTRY is docker.io

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

echo "Docker login failed. Retrying"
fi
done
set -ex
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it exit with non-zero code if it never succeeds?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, updated the script.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will never be called?

@XinShuYang
Copy link
Contributor Author

/test-all

@XinShuYang
Copy link
Contributor Author

/test-all

echo "Docker login failed. Retrying"
fi
done
set -ex
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will never be called?

esac
done

function docker_login() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is it called?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, corrected it.

@XinShuYang
Copy link
Contributor Author

/test-all

@XinShuYang XinShuYang requested a review from tnqn May 30, 2024 04:52
Enhance Jenkins pipeline by integrating Docker credentials for login to avoid rate limit issues

Signed-off-by: Shuyang Xin <[email protected]>
@XinShuYang
Copy link
Contributor Author

/test-all

Copy link
Contributor

@antoninbas antoninbas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a commit to fix some issues in docker_login.sh
I want to merge this ASAP to restore CI

@antoninbas
Copy link
Contributor

/test-all

@antoninbas antoninbas merged commit f7cce73 into antrea-io:main May 30, 2024
52 of 56 checks passed
luolanzone pushed a commit to luolanzone/antrea that referenced this pull request Jun 13, 2024
…io#6368)

* Add Docker login to Jenkins script to avoid rate limit issue

Enhance Jenkins pipeline by integrating Docker credentials for login to avoid rate limit issues

Signed-off-by: Shuyang Xin <[email protected]>

* Fix some issues in docker_login.sh

Signed-off-by: Antonin Bas <[email protected]>

---------

Signed-off-by: Shuyang Xin <[email protected]>
Signed-off-by: Antonin Bas <[email protected]>
Co-authored-by: Antonin Bas <[email protected]>
luolanzone pushed a commit to luolanzone/antrea that referenced this pull request Jun 13, 2024
…io#6368)

* Add Docker login to Jenkins script to avoid rate limit issue

Enhance Jenkins pipeline by integrating Docker credentials for login to avoid rate limit issues

Signed-off-by: Shuyang Xin <[email protected]>

* Fix some issues in docker_login.sh

Signed-off-by: Antonin Bas <[email protected]>

---------

Signed-off-by: Shuyang Xin <[email protected]>
Signed-off-by: Antonin Bas <[email protected]>
Co-authored-by: Antonin Bas <[email protected]>
tnqn pushed a commit that referenced this pull request Jun 13, 2024
…6439)

* Add Docker login to Jenkins script to avoid rate limit issue

Enhance Jenkins pipeline by integrating Docker credentials for login to avoid rate limit issues

* Fix some issues in docker_login.sh

Signed-off-by: Shuyang Xin <[email protected]>
Signed-off-by: Antonin Bas <[email protected]>
tnqn pushed a commit that referenced this pull request Jun 13, 2024
…6440)

* Add Docker login to Jenkins script to avoid rate limit issue

Enhance Jenkins pipeline by integrating Docker credentials for login to avoid rate limit issues

* Fix some issues in docker_login.sh

Signed-off-by: Shuyang Xin <[email protected]>
Signed-off-by: Antonin Bas <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants