-
Notifications
You must be signed in to change notification settings - Fork 9
194 lines (168 loc) · 8.31 KB
/
copy_csp_blocks.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
name: Copy CSP Blocks
# Upon merge of a PR in which 'server/configs/application.properties' was updated...
# this workflow copies the Content Security Policy blocks to other repos, as marked by:
# start: "## START OF CSP COPY BLOCK" or "## START OF CSP ENFORCE BLOCK" (to only copy and uncomment the csp.enforce section)
# end: "## END OF CSP COPY BLOCK" or "## END OF CSP ENFORCE BLOCK"
# note: if the contents between the start/end have not changed, it's a no-op, and no PRs are pushed to the target repos
on:
pull_request:
types:
- closed
branches:
- develop
paths:
- server/configs/application.properties
jobs:
copy_csp:
if: github.event.pull_request.merged
runs-on: ubuntu-latest
outputs:
csp_report_on: ${{ steps.cspvars.outputs.csp_report_on }}
csp_enforce_off: ${{ steps.cspvars.outputs.csp_enforce_off }}
csp_enforce_on: ${{ steps.cspvars.outputs.csp_enforce_on }}
steps:
- name: Check Out Code
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Copy CSP blocks into vars
id: cspvars
run: |
# report block, fixing report-uri for non-teamcity usage:
CSP_REPORT_ON=$( awk '/## END OF CSP REPORT BLOCK/{p=0};p;/## START OF CSP REPORT BLOCK/{p=1}' server/configs/application.properties |\
sed 's/report-uri /report-uri https:\/\/www.labkey.org/' )
# enforce block, fixing report-uri for non-teamcity usage, removing useLocalBuild comment, adding upgrade-insecure-requests:
CSP_ENFORCE_OFF=$( awk '/## END OF CSP ENFORCE BLOCK/{p=0};p;/## START OF CSP ENFORCE BLOCK/{p=1}' server/configs/application.properties |\
sed 's/^#useLocalBuild#/# /' |\
sed '/base-uri/a# upgrade-insecure-requests ;\\' |\
sed 's/report-uri /report-uri https:\/\/www.labkey.org/' )
# enforce block, uncommented:
CSP_ENFORCE_ON=$( awk '/## END OF CSP ENFORCE BLOCK/{p=0};p;/## START OF CSP ENFORCE BLOCK/{p=1}' server/configs/application.properties |\
sed 's/^#useLocalBuild#//' |\
sed '/base-uri/a\ upgrade-insecure-requests ;\\' |\
sed 's/report-uri /report-uri https:\/\/www.labkey.org/' )
# use unique delimiter for multiline outputs: https://stackoverflow.com/a/74256214
delimiter="$(openssl rand -hex 8)"
echo "csp_report_on<<${delimiter}" >> "${GITHUB_OUTPUT}"
echo "$CSP_REPORT_ON" >> "${GITHUB_OUTPUT}"
echo "${delimiter}" >> "${GITHUB_OUTPUT}"
echo "csp_enforce_off<<${delimiter}" >> "${GITHUB_OUTPUT}"
echo "$CSP_ENFORCE_OFF" >> "${GITHUB_OUTPUT}"
echo "${delimiter}" >> "${GITHUB_OUTPUT}"
echo "csp_enforce_on<<${delimiter}" >> "${GITHUB_OUTPUT}"
echo "$CSP_ENFORCE_ON" >> "${GITHUB_OUTPUT}"
echo "${delimiter}" >> "${GITHUB_OUTPUT}"
paste_csp_into_chef_repo:
needs: copy_csp
runs-on: ubuntu-latest
env:
csp_report_on: ${{ needs.copy_csp.outputs.csp_report_on }}
csp_enforce_off: ${{ needs.copy_csp.outputs.csp_enforce_off }}
ap_file: "cookbooks/lk_appserver/templates/default/application.properties.erb"
steps:
- name: Check out repo
uses: actions/checkout@v4
with:
repository: LabKey/syseng-chef-server
token: ${{ secrets.TERRAFORM_TOKEN }}
- name: Paste Into Chef Repo
run: |
printf "\n\n>>>> $ap_file before I change it: <<<<\n\n"
cat $ap_file
printf "\n\n>>>> caught csp_report_on env var: <<<<\n$csp_report_on n\n"
printf "\n\n>>>> caught csp_enforce_off env var: <<<<\n$csp_enforce_off n\n"
printf "\n\n>>>> replacing csp blocks in $ap_file <<<<\n\n"
python <<EOF
import os, sys, re
fname = os.environ.get('ap_file')
os.rename(fname, fname + '.orig')
with open(fname + '.orig', 'r') as fin, open(fname, 'w') as fout:
data = fin.read()
data = re.sub(r'(## START OF CSP REPORT BLOCK \\(DO NOT CHANGE THIS TEXT\\)).*?(## END OF CSP REPORT BLOCK \\(DO NOT CHANGE THIS TEXT\\))',
r'\1\n' +
os.environ.get('csp_report_on') +
r'\n\2', data, flags=re.DOTALL)
data = re.sub(r'(## START OF CSP ENFORCE BLOCK \\(DO NOT CHANGE THIS TEXT\\)).*?(## END OF CSP ENFORCE BLOCK \\(DO NOT CHANGE THIS TEXT\\))',
r'\1\n' +
os.environ.get('csp_enforce_off') +
r'\n\2', data, flags=re.DOTALL)
fout.write(data)
EOF
printf "\n\n>>>> updated $ap_file: <<<<\n\n"
cat $ap_file
git status
if [[ $(git diff-index --name-only HEAD |grep application.properties) ]]; then
printf "\n\n>>>> changes detected, so updating chef recipe version <<<<\n\n"
NEW_VER=$(grep version cookbooks/lk_appserver/metadata.rb |cut -d '"' -f 2 |awk -F. -v OFS=. '{$NF += 1 ; print}')
sed -i 's/\(version.*\)".*"/\1"'$NEW_VER'"/' cookbooks/lk_appserver/metadata.rb
sed -i 's/\(lk_appserver .*\)([0-9]*.[0-9]*.[0-9]*)/\1('$NEW_VER')/' Berksfile.lock
fi
- name: Create Pull Request
id: cpr
uses: peter-evans/create-pull-request@v7
with:
token: ${{ secrets.TERRAFORM_TOKEN }}
branch: fb_update_csp_per_${{ github.sha }}
title: "update CSP to match commit ${{ github.sha }}"
body: "update CSP to match commit ${{ github.sha }}"
commit-message: "update CSP to match commit ${{ github.sha }}"
add-paths: |
${{ env.ap_file }}
cookbooks/lk_appserver/metadata.rb
Berksfile.lock
- name: Check outputs
if: ${{ steps.cpr.outputs.pull-request-number }}
run: |
echo "Chef Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}" >> $GITHUB_STEP_SUMMARY
paste_csp_into_dockerfile_repo:
needs: copy_csp
runs-on: ubuntu-latest
env:
csp_report_on: ${{ needs.copy_csp.outputs.csp_report_on }}
csp_enforce_on: ${{ needs.copy_csp.outputs.csp_enforce_on }}
ap_file: "application.properties"
steps:
- name: Check out repo
uses: actions/checkout@v4
with:
repository: Labkey/Dockerfile
token: ${{ secrets.TERRAFORM_TOKEN }}
- name: Paste Into Dockerfile Repo
run: |
printf "\n\n>>>> $ap_file before I change it: <<<<\n\n"
cat $ap_file
printf "\n\n>>>> caught csp_report_on env var: <<<<\n$csp_report_on n\n"
printf "\n\n>>>> caught csp_enforce_on env var:<<<<\n$csp_enforce_on\n\n"
printf "\n\n>>>> replacing csp block in $ap_file <<<<\n\n"
python <<EOF
import os, sys, re
fname = os.environ.get('ap_file')
os.rename(fname, fname + '.orig')
with open(fname + '.orig', 'r') as fin, open(fname, 'w') as fout:
data = fin.read()
data = re.sub(r'(## START OF CSP REPORT BLOCK \\(DO NOT CHANGE THIS TEXT\\)).*?(## END OF CSP REPORT BLOCK \\(DO NOT CHANGE THIS TEXT\\))',
r'\1\n' +
os.environ.get('csp_report_on') +
r'\n\2', data, flags=re.DOTALL)
data = re.sub(r'(## START OF CSP ENFORCE BLOCK \\(DO NOT CHANGE THIS TEXT\\)).*?(## END OF CSP ENFORCE BLOCK \\(DO NOT CHANGE THIS TEXT\\))',
r'\1\n' +
os.environ.get('csp_enforce_on') +
r'\n\2', data, flags=re.DOTALL)
fout.write(data)
EOF
printf "\n\n>>>> updated $ap_file: <<<<\n\n"
cat $ap_file
- name: Create Pull Request
id: cpr
uses: peter-evans/create-pull-request@v7
with:
token: ${{ secrets.TERRAFORM_TOKEN }}
branch: fb_update_csp_per_${{ github.sha }}
title: "update CSP to match commit ${{ github.sha }}"
body: "update CSP to match commit ${{ github.sha }}"
commit-message: "update CSP to match commit ${{ github.sha }}"
add-paths: ${{ env.ap_file }}
- name: Check outputs
if: ${{ steps.cpr.outputs.pull-request-number }}
run: |
echo "Dockerfile Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}" >> $GITHUB_STEP_SUMMARY