Skip to content

Commit 65bdeec

Browse files
Docker registry auth support via driver config.
Signed-off-by: Shishir Mahajan <[email protected]>
1 parent 6f20bcf commit 65bdeec

File tree

5 files changed

+43
-32
lines changed

5 files changed

+43
-32
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ More detailed instructions are in the [`example README.md`](https://github.com/R
8787
| **containerd_runtime** | string | yes | N/A | Runtime for containerd e.g. `io.containerd.runc.v1` or `io.containerd.runc.v2`. |
8888
| **stats_interval** | string | no | 1s | Interval for collecting `TaskStats`. |
8989
| **allow_privileged** | bool | no | true | If set to `false`, driver will deny running privileged jobs. |
90+
| **auth** | block | no | N/A | Provide authentication for a private registry. See [Authentication](#authentication-private-registry) for more details. |
9091

9192
**Task Config**
9293

@@ -111,7 +112,7 @@ More detailed instructions are in the [`example README.md`](https://github.com/R
111112
| **cap_add** | []string | no | Add individual capabilities. |
112113
| **cap_drop** | []string | no | Drop invidual capabilities. |
113114
| **devices** | []string | no | A list of devices to be exposed to the container. |
114-
| **auth** | block | no | Provide authentication for a private registry. See [Auth](#auth) for more details. |
115+
| **auth** | block | no | Provide authentication for a private registry. See [Authentication](#authentication-private-registry) for more details. |
115116
| **mounts** | []block | no | A list of mounts to be mounted in the container. Volume, bind and tmpfs type mounts are supported. fstab style [`mount options`](https://github.com/containerd/containerd/blob/master/mount/mount_linux.go#L211-L235) are supported. |
116117

117118
**Mount block**<br/>
@@ -163,18 +164,19 @@ config {
163164
}
164165
```
165166

166-
### auth
167+
## Authentication (Private registry)
167168

168-
If you want to pull from a private repository e.g. docker hub, you can specify `username` and `password` in the `auth` stanza. See example below.
169+
`auth` stanza allow you to set credentials for your private registry e.g. if you want to pull
170+
an image from a private repository in docker hub.<br/>
171+
`auth` stanza can be set either in `Driver Config` or `Task Config` or both.<br/>
172+
If set at both places, `Task Config` auth will take precedence over `Driver Config` auth.
169173

170-
**NOTE**: In the below example, `user` and `pass` are just placeholder values which need to be replaced by actual `username` and `password`, when specifying the credentials.
174+
**NOTE**: In the below example, `user` and `pass` are just placeholder values which need to be replaced by actual `username` and `password`, when specifying the credentials. Below `auth` stanza can be used for both `Driver Config` and `Task Config`.
171175

172176
```
173-
config {
174-
auth {
177+
auth {
175178
username = "user"
176179
password = "pass"
177-
}
178180
}
179181
```
180182

containerd/containerd.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,20 @@ func (d *Driver) getContainerdVersion() (containerd.Version, error) {
6565

6666
type CredentialsOpt func(string) (string, string, error)
6767

68-
func parshAuth(auth *RegistryAuth) CredentialsOpt {
68+
func (d *Driver) parshAuth(auth *RegistryAuth) CredentialsOpt {
6969
return func(string) (string, string, error) {
70-
if auth == nil {
71-
return "", "", nil
70+
var username, password string
71+
if d.config.Auth.Username != "" && d.config.Auth.Password != "" {
72+
username = d.config.Auth.Username
73+
password = d.config.Auth.Password
7274
}
73-
return auth.Username, auth.Password, nil
75+
76+
// Job auth will take precedence over plugin auth options.
77+
if auth.Username != "" && auth.Password != "" {
78+
username = auth.Username
79+
password = auth.Password
80+
}
81+
return username, password, nil
7482
}
7583
}
7684

@@ -98,7 +106,7 @@ func (d *Driver) pullImage(imageName, imagePullTimeout string, auth *RegistryAut
98106

99107
pullOpts := []containerd.RemoteOpt{
100108
containerd.WithPullUnpack,
101-
withResolver(parshAuth(auth)),
109+
withResolver(d.parshAuth(auth)),
102110
}
103111

104112
return d.client.Pull(ctxWithTimeout, named.String(), pullOpts...)

containerd/driver.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ var (
8484
hclspec.NewAttr("allow_privileged", "bool", false),
8585
hclspec.NewLiteral("true"),
8686
),
87+
"auth": hclspec.NewBlock("auth", false, hclspec.NewObject(map[string]*hclspec.Spec{
88+
"username": hclspec.NewAttr("username", "string", true),
89+
"password": hclspec.NewAttr("password", "string", true),
90+
})),
8791
})
8892

8993
// taskConfigSpec is the specification of the plugin's configuration for
@@ -117,8 +121,8 @@ var (
117121
"readonly_rootfs": hclspec.NewAttr("readonly_rootfs", "bool", false),
118122
"host_network": hclspec.NewAttr("host_network", "bool", false),
119123
"auth": hclspec.NewBlock("auth", false, hclspec.NewObject(map[string]*hclspec.Spec{
120-
"username": hclspec.NewAttr("username", "string", false),
121-
"password": hclspec.NewAttr("password", "string", false),
124+
"username": hclspec.NewAttr("username", "string", true),
125+
"password": hclspec.NewAttr("password", "string", true),
122126
})),
123127
"mounts": hclspec.NewBlockList("mounts", hclspec.NewObject(map[string]*hclspec.Spec{
124128
"type": hclspec.NewDefault(
@@ -144,10 +148,11 @@ var (
144148

145149
// Config contains configuration information for the plugin
146150
type Config struct {
147-
Enabled bool `codec:"enabled"`
148-
ContainerdRuntime string `codec:"containerd_runtime"`
149-
StatsInterval string `codec:"stats_interval"`
150-
AllowPrivileged bool `codec:"allow_privileged"`
151+
Enabled bool `codec:"enabled"`
152+
ContainerdRuntime string `codec:"containerd_runtime"`
153+
StatsInterval string `codec:"stats_interval"`
154+
AllowPrivileged bool `codec:"allow_privileged"`
155+
Auth RegistryAuth `codec:"auth"`
151156
}
152157

153158
// Volume, bind, and tmpfs type mounts are supported.

tests/004-test-privileged.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,17 @@ test_privileged_nomad_job() {
3333

3434
# Check if container is running in privileged mode.
3535
echo "INFO: Checking if container is running in privileged mode."
36+
37+
# If you are running the tests locally in the vagrant VM (Ubuntu 18.04.03)
38+
# the capability set (capsh --print) consists of 37 capabilities.
39+
# However, GHA environment is showing 39 capabilities.
40+
# The below check will set the expected_capabilities to 37 or 39
41+
# depending on the execution environment.
3642
expected_capabilities="37"
43+
if [[ "$GITHUB_ACTIONS" == "true" ]]; then
44+
expected_capabilities="39"
45+
fi
46+
3747
actual_capabilities=$(nomad alloc exec -job privileged capsh --print|grep -i bounding|cut -d '=' -f 2|awk '{split($0,a,","); print a[length(a)]}')
3848
if [ "$expected_capabilities" != "$actual_capabilities" ]; then
3949
echo "ERROR: container is not running in privileged mode."

tests/run_tests.sh

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,6 @@ setup() {
8080
return 0
8181
fi
8282

83-
sudo systemctl stop apt-daily-upgrade apt-daily >/dev/null 2>&1
84-
85-
set +e
86-
sudo pkill --signal SIGKILL -P $(ps faux | grep 'daily' | awk '{print $2}')
87-
set -e
88-
89-
# Remove docker daemon and containerd.
90-
sudo systemctl stop docker
91-
sudo systemctl stop containerd
92-
sudo apt-get purge -y docker-ce docker-ce-cli containerd.io
93-
94-
sudo apt-get update
95-
sudo apt-get install -y apt-utils curl runc unzip make build-essential
96-
9783
# Change $(pwd) to /tmp
9884
pushd /tmp
9985

0 commit comments

Comments
 (0)