1
1
#! /usr/bin/env bash
2
2
3
3
# Requirements
4
- # - curl
4
+ # - crane
5
5
# - jq
6
6
# - awk
7
7
#
8
8
# ./examples/credential_helper/auth.sh <<< '{"uri":"https://public.ecr.aws/token/?scope\u003drepository:lambda/python:pull\u0026service\u003dpublic.ecr.aws"}'
9
9
# ./examples/credential_helper/auth.sh <<< '{"uri":"https://public.ecr.aws/v2/lambda/python/manifests/3.11.2024.01.25.10"}'
10
+
10
11
function log () {
11
- echo " $1 " >> /tmp/oci_auth.log
12
+ echo $@ >> " /tmp/oci_auth.log"
12
13
}
13
14
14
15
log " "
@@ -20,18 +21,39 @@ log "Payload: $input"
20
21
uri=$( jq -r " .uri" <<< $input )
21
22
log " URI: $uri "
22
23
23
- host=" $( echo $uri | awk -F[/:] ' {print $4}' ) "
24
+ host=" $( awk -F[/:] ' {print $4}' <<< $uri ) "
24
25
log " Host: $host "
25
26
27
+
26
28
if [[ $input == * " /token" * ]]; then
27
29
log " Auth: None"
28
30
echo " {}"
29
- exit 0
31
+ exit 1
30
32
fi
31
33
34
+ repository=$( awk -F' ^https?://|v2/|/manifests|/blobs' ' {print $2 $3}' <<< " $uri" )
35
+ log " Repository: $repository "
36
+
37
+
38
+ ACCEPTED_MEDIA_TYPES=' [
39
+ "application/vnd.docker.distribution.manifest.v2+json",
40
+ "application/vnd.docker.distribution.manifest.list.v2+json",
41
+ "application/vnd.oci.image.manifest.v1+json",
42
+ "application/vnd.oci.image.index.v1+json"
43
+ ]'
44
+
45
+
32
46
# This will write the response to stdout in a format that Bazels credential helper protocol understands.
33
47
# Since this is called by Bazel, users won't bee seeing output of this.
34
- curl -fsSL https://$host /token | jq ' {headers:{"Authorization": [("Bearer " + .token)]}}'
48
+ crane auth token " $repository " |
49
+ jq --argjson accept " $ACCEPTED_MEDIA_TYPES " \
50
+ ' {headers: {Authorization: [("Bearer " + .token)], Accept: [($accept | join(", "))], "Docker-Distribution-API-Version": ["registry/2.0"] }}'
51
+
52
+ if [[ $? != 0 ]]; then
53
+ log " Auth: Failed"
54
+ exit 1
55
+ fi
35
56
log " Auth: Complete"
36
57
37
- # Alternatively you can call an external program such as `docker-credential-ecr-login` to perform the token exchange.
58
+
59
+ # Alternatively you can call an external program such as `docker-credential-ecr-login` to perform the token exchange.
0 commit comments