Commit 7b70063 1 parent 2460897 commit 7b70063 Copy full SHA for 7b70063
File tree 10 files changed +85
-47
lines changed
utils-rotate-docker-configs
utils-rotate-docker-secrets
10 files changed +85
-47
lines changed Original file line number Diff line number Diff line change 19
19
# Allows you to run this workflow manually from the Actions tab
20
20
workflow_dispatch :
21
21
22
+ # Trigger on main to clear the lock
23
+ push :
24
+ branches : [ "main" ]
25
+
22
26
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
23
27
jobs :
24
28
Original file line number Diff line number Diff line change 9
9
dynamic_dns {
10
10
provider digitalocean {env.DIGITALOCEAN_API_TOKEN}
11
11
domains {
12
- {{domain} } @ www
12
+ {$DOMAIN } @ www
13
13
}
14
14
dynamic_domains
15
15
}
20
20
authentication portal myportal {
21
21
crypto default token lifetime 3600
22
22
crypto key sign-verify {env.JWT_SHARED_KEY}
23
- cookie domain {{domain} }
23
+ cookie domain {$DOMAIN }
24
24
enable identity provider github
25
25
ui {
26
26
links {
36
36
}
37
37
38
38
authorization policy admins_policy {
39
- set auth url https://auth.{{domain} }/oauth2/github
39
+ set auth url https://auth.{$DOMAIN }/oauth2/github
40
40
crypto key verify {env.JWT_SHARED_KEY}
41
41
allow roles authp/admin authp/user
42
42
validate bearer header
58
58
}
59
59
60
60
# Snippet enable automatic DNS configuration
61
- (external- dns) {
61
+ (dns-challenge ) {
62
62
tls {
63
63
dns digitalocean {env.DIGITALOCEAN_API_TOKEN}
64
64
}
65
65
}
66
66
67
67
# Auth endpoint for caddy security
68
- auth .{{domain} } {
69
- import external- dns
68
+ auth .{$DOMAIN } {
69
+ import dns-challenge
70
70
authenticate with myportal
71
71
}
72
72
Original file line number Diff line number Diff line change @@ -17,15 +17,18 @@ services:
17
17
- caddy
18
18
volumes :
19
19
- /var/run/docker.sock:/var/run/docker.sock
20
- - " {{ caddy_dir }}/Caddyfile:/etc/caddy/Caddyfile"
21
20
- caddy_data:/data
22
21
- caddy_config:/config
22
+ configs :
23
+ - source : caddy_global_caddyfile
24
+ target : " /etc/caddy/Caddyfile"
23
25
secrets :
24
26
- caddy_github_client_id
25
27
- caddy_github_client_secret
26
28
- caddy_jwt_shared_key
27
29
- caddy_digitalocean_api_token
28
30
environment :
31
+ DOMAIN : " {{ domain }}"
29
32
GITHUB_CLIENT_ID_FILE : /run/secrets/caddy_github_client_id
30
33
GITHUB_CLIENT_SECRET_FILE : /run/secrets/caddy_github_client_secret
31
34
JWT_SHARED_KEY_FILE : /run/secrets/caddy_jwt_shared_key
@@ -58,6 +61,10 @@ volumes:
58
61
caddy_data :
59
62
caddy_config :
60
63
64
+ configs :
65
+ caddy_global_caddyfile :
66
+ external : true
67
+
61
68
networks :
62
69
caddy :
63
70
attachable : true
Original file line number Diff line number Diff line change 13
13
state : directory
14
14
mode : ' 0644'
15
15
16
+ - name : Define asssets folder
17
+ set_fact :
18
+ assets_path : " {{ role_path }}/assets"
19
+
16
20
- name : Copy Compose file to remote server
17
21
template :
18
22
src : " {{ item }}"
19
23
dest : " {{ caddy_dir }}"
20
24
mode : ' 0644'
21
25
with_fileglob :
22
- - " {{ role_path }}/assets /*-stack.yml"
23
- - " {{ role_path }}/assets /Caddyfile"
24
- - " {{ role_path }}/assets /Dockerfile"
26
+ - " {{ assets_path }}/*-stack.yml"
27
+ - " {{ assets_path }}/Caddyfile"
28
+ - " {{ assets_path }}/Dockerfile"
25
29
26
30
# ##
27
31
# Create Caddy Pre-requisits
28
32
# ##
33
+ - name : Manager Caddy Configs
34
+ include_role :
35
+ name : utils-rotate-docker-configs
36
+ vars :
37
+ docker_compose_path : " {{ caddy_dir }}/caddy-stack.yml"
38
+ configs :
39
+ - {name: 'caddy_global_caddyfile', file_path: "{{ caddy_dir }}/Caddyfile"}
40
+
29
41
- name : Manager Caddy Secrets
30
42
include_role :
31
43
name : utils-rotate-docker-secrets
Original file line number Diff line number Diff line change
1
+ docker_compose_path : " {{ undef(hint='You must specify docker-compose file to update') }}"
Original file line number Diff line number Diff line change
1
+ - name : List all Docker configs managed by this role
2
+ command : docker config ls --filter label=managed_by=rotate_docker_configs --format "{{ '{{ .Name }}' }}"
3
+ register : existing_configs
4
+ changed_when : false
5
+
6
+ - name : Identify configs to keep
7
+ set_fact :
8
+ configs_to_keep : " {{ configs_to_keep | default([]) + [item.name + '_' + config_checksums[item.name]] }}"
9
+ loop : " {{ configs }}"
10
+
11
+ - name : Remove dangling configs
12
+ docker_config :
13
+ name : " {{ item }}"
14
+ state : absent
15
+ when : item not in configs_to_keep
16
+ loop : " {{ existing_configs.stdout_lines }}"
17
+ ignore_errors : true
18
+ register : ignore_errors_register
Original file line number Diff line number Diff line change
1
+ dependencies : []
2
+ # List your role dependencies here, one per line. Be sure to remove the '[]' above,
3
+ # if you add dependencies to this list.
Original file line number Diff line number Diff line change
1
+ - name : Gather file stats and checksums
2
+ stat :
3
+ path : " {{ item.file_path }}"
4
+ checksum : md5
5
+ loop : " {{ configs }}"
6
+ register : file_stats
7
+
8
+ - name : Create dictionary of checksums
9
+ set_fact :
10
+ config_checksums : " {{ config_checksums | default({}) | combine({ item.name: file_stats.results[idx].stat.checksum }) }}"
11
+ loop : " {{ configs }}"
12
+ loop_control :
13
+ index_var : idx
14
+
15
+ - name : Create new configs if value has changed
16
+ docker_config :
17
+ name : " {{ item.name }}_{{ config_checksums[item.name] }}"
18
+ data_src : " {{ item.file_path }}"
19
+ state : present
20
+ labels :
21
+ managed_by : " rotate_docker_configs"
22
+ name : " {{ item.name }}"
23
+ loop : " {{ configs }}"
24
+
25
+ - name : Replace config names in Docker Compose file
26
+ replace :
27
+ path : " {{ docker_compose_path }}"
28
+ regexp : " {{ item.name }}(_[a-f0-9]{32})?"
29
+ replace : " {{ item.name }}_{{ config_checksums[item.name] }}"
30
+ loop : " {{ configs }}"
Original file line number Diff line number Diff line change 1
- checksum_directory : /var/data/ansible
2
1
docker_compose_path : " {{ undef(hint='You must specify docker-compose file to update') }}"
Original file line number Diff line number Diff line change 1
- - name : Ensure checksum directory exists
2
- file :
3
- path : " {{ checksum_directory }}"
4
- mode : ' 0644'
5
- state : directory
6
-
7
1
- name : Calculate checksums for secrets
8
2
set_fact :
9
3
secret_checksums : " {{ secret_checksums
13
7
| cut -d\" \" -f1')}) }}"
14
8
loop : " {{ secrets }}"
15
9
16
- - name : Check if previous checksums file exists
17
- stat :
18
- path : " {{ checksum_directory }}/secrets.ini"
19
- register : previous_checksums_file
20
-
21
- - name : Load previous checksums if file exists
22
- set_fact :
23
- previous_checksums : " {{ previous_checksums
24
- | default({})
25
- | combine({
26
- item.name: lookup(
27
- 'ansible.builtin.ini',
28
- 'checksum',
29
- section=item.name,
30
- file=checksum_directory + '/secrets.ini'
31
- )
32
- })
33
- }}"
34
- loop : " {{ secrets }}"
35
- when : previous_checksums_file.stat.exists
36
-
37
10
- name : Create new secrets if value has changed
38
11
docker_secret :
39
12
name : " {{ item.name }}_{{ secret_checksums[item.name] }}"
45
18
when : (previous_checksums[item.name] is not defined) or (previous_checksums[item.name] != secret_checksums[item.name])
46
19
loop : " {{ secrets }}"
47
20
48
- - name : Update checksums file
49
- ini_file :
50
- path : ./secrets.ini
51
- section : " {{ item.name }}"
52
- option : checksum
53
- value : " {{ secret_checksums[item.name] }}"
54
- mode : ' 0644'
55
- loop : " {{ secrets }}"
56
-
57
21
- name : Replace secret names in Docker Compose file
58
22
replace :
59
23
path : " {{ docker_compose_path }}"
You can’t perform that action at this time.
0 commit comments