Skip to content

Commit f4dd218

Browse files
authored
feat: make UID, GID, & mode for secrets and configs configurable (kreuzwerker#231)
Closes kreuzwerker#216 * feat(service): makes uid, gid and file mode configurable * docs(service): updates config and secret configuration
1 parent 546c6ae commit f4dd218

5 files changed

Lines changed: 73 additions & 7 deletions

File tree

docker/resource_docker_service.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,25 @@ func resourceDockerService() *schema.Resource {
423423
Description: "Represents the final filename in the filesystem",
424424
Required: true,
425425
},
426+
"file_uid": {
427+
Type: schema.TypeString,
428+
Description: "Represents the file UID",
429+
Optional: true,
430+
Default: "0",
431+
},
432+
"file_gid": {
433+
Type: schema.TypeString,
434+
Description: "Represents the file GID",
435+
Optional: true,
436+
Default: "0",
437+
},
438+
"file_mode": {
439+
Type: schema.TypeInt,
440+
Description: "Represents represents the FileMode of the file",
441+
Optional: true,
442+
Default: 0444,
443+
ValidateFunc: validateIntegerGeqThan(0),
444+
},
426445
},
427446
},
428447
},
@@ -447,6 +466,25 @@ func resourceDockerService() *schema.Resource {
447466
Description: "Represents the final filename in the filesystem",
448467
Required: true,
449468
},
469+
"file_uid": {
470+
Type: schema.TypeString,
471+
Description: "Represents the file UID",
472+
Optional: true,
473+
Default: "0",
474+
},
475+
"file_gid": {
476+
Type: schema.TypeString,
477+
Description: "Represents the file GID",
478+
Optional: true,
479+
Default: "0",
480+
},
481+
"file_mode": {
482+
Type: schema.TypeInt,
483+
Description: "Represents represents the FileMode of the file",
484+
Optional: true,
485+
Default: 0444,
486+
ValidateFunc: validateIntegerGeqThan(0),
487+
},
450488
},
451489
},
452490
},

docker/resource_docker_service_funcs.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -893,13 +893,14 @@ func createContainerSpec(v interface{}) (*swarm.ContainerSpec, error) {
893893

894894
for _, rawSecret := range value.(*schema.Set).List() {
895895
rawSecret := rawSecret.(map[string]interface{})
896+
rawFilemode := rawSecret["file_mode"].(int)
896897
secret := swarm.SecretReference{
897898
SecretID: rawSecret["secret_id"].(string),
898899
File: &swarm.SecretReferenceFileTarget{
899900
Name: rawSecret["file_name"].(string),
900-
UID: "0",
901-
GID: "0",
902-
Mode: os.FileMode(0444),
901+
UID: rawSecret["file_uid"].(string),
902+
GID: rawSecret["file_gid"].(string),
903+
Mode: os.FileMode(uint32(rawFilemode)),
903904
},
904905
}
905906
if value, ok := rawSecret["secret_name"]; ok {
@@ -914,13 +915,14 @@ func createContainerSpec(v interface{}) (*swarm.ContainerSpec, error) {
914915

915916
for _, rawConfig := range value.(*schema.Set).List() {
916917
rawConfig := rawConfig.(map[string]interface{})
918+
rawFilemode := rawConfig["file_mode"].(int)
917919
config := swarm.ConfigReference{
918920
ConfigID: rawConfig["config_id"].(string),
919921
File: &swarm.ConfigReferenceFileTarget{
920922
Name: rawConfig["file_name"].(string),
921-
UID: "0",
922-
GID: "0",
923-
Mode: os.FileMode(0444),
923+
UID: rawConfig["file_uid"].(string),
924+
GID: rawConfig["file_gid"].(string),
925+
Mode: os.FileMode(uint32(rawFilemode)),
924926
},
925927
}
926928
if value, ok := rawConfig["config_name"]; ok {

docker/resource_docker_service_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,10 @@ func TestAccDockerService_fullSpec(t *testing.T) {
306306
secrets {
307307
secret_id = "${docker_secret.service_secret.id}"
308308
secret_name = "${docker_secret.service_secret.name}"
309-
file_name = "/secrets.json"
309+
file_name = "/secrets.json"
310+
file_uid = "0"
311+
file_gid = "0"
312+
file_mode = 0777
310313
}
311314
312315
configs {

docker/structures_service.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,13 @@ func flattenServiceSecrets(in []*swarm.SecretReference) *schema.Set {
315315
}
316316
if v.File != nil {
317317
m["file_name"] = v.File.Name
318+
if len(v.File.UID) > 0 {
319+
m["file_uid"] = v.File.UID
320+
}
321+
if len(v.File.GID) > 0 {
322+
m["file_gid"] = v.File.GID
323+
}
324+
m["file_mode"] = int(v.File.Mode)
318325
}
319326
out[i] = m
320327
}
@@ -335,6 +342,13 @@ func flattenServiceConfigs(in []*swarm.ConfigReference) *schema.Set {
335342
}
336343
if v.File != nil {
337344
m["file_name"] = v.File.Name
345+
if len(v.File.UID) > 0 {
346+
m["file_uid"] = v.File.UID
347+
}
348+
if len(v.File.GID) > 0 {
349+
m["file_gid"] = v.File.GID
350+
}
351+
m["file_mode"] = int(v.File.Mode)
338352
}
339353
out[i] = m
340354
}

website/docs/r/service.html.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ resource "docker_service" "foo" {
147147
secret_id = "${docker_secret.service_secret.id}"
148148
secret_name = "${docker_secret.service_secret.name}"
149149
file_name = "/secrets.json"
150+
file_uid = "0"
151+
file_gid = "0"
152+
file_mode = 0777
150153
},
151154
]
152155
@@ -410,6 +413,9 @@ the extra mount mappings for the container. Each `secrets` block is a reference
410413
* `secret_id` - (Required, string) ConfigID represents the ID of the specific secret.
411414
* `secret_name` - (Optional, string) The name of the secret that this references, but internally it is just provided for lookup/display purposes
412415
* `file_name` - (Required, string) Represents the final filename in the filesystem. The specific target file that the secret data is written within the docker container, e.g. `/root/secret/secret.json`
416+
* `file_uid` - (Optional, string) Represents the file UID. Defaults: `0`
417+
* `file_gid` - (Optional, string) Represents the file GID. Defaults: `0`
418+
* `file_mode` - (Optional, int) Represents the FileMode of the file. Defaults: `0444`
413419

414420
<a id="configs-1"></a>
415421
### Configs
@@ -420,6 +426,9 @@ the extra mount mappings for the container. Each `configs` is a reference to a s
420426
* `config_id` - (Required, string) ConfigID represents the ID of the specific config.
421427
* `config_name` - (Optional, string) The name of the config that this references, but internally it is just provided for lookup/display purposes
422428
* `file_name` - (Required, string) Represents the final filename in the filesystem. The specific target file that the config data is written within the docker container, e.g. `/root/config/config.json`
429+
* `file_uid` - (Optional, string) Represents the file UID. Defaults: `0`
430+
* `file_gid` - (Optional, string) Represents the file GID. Defaults: `0`
431+
* `file_mode` - (Optional, int) Represents the FileMode of the file. Defaults: `0444`
423432

424433
<!-- end task-container-spec -->
425434

0 commit comments

Comments
 (0)