-
Notifications
You must be signed in to change notification settings - Fork 0
158 lines (151 loc) · 6.56 KB
/
issue-ops-sign-ssh-key.yaml
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
on:
issues:
types: [opened]
name: IssueOps - Sign ssh key
jobs:
prechecks:
if: startsWith(github.event.issue.title, 'Sign ssh key')
name: Pre-Check
runs-on: ubuntu-latest
outputs:
recipient: ${{ steps.prechecks.outputs.recipient }}
key: ${{ steps.prechecks.outputs.key }}
org: ${{ steps.prechecks.outputs.org }}
algo: ${{ steps.prechecks.outputs.algo }}
steps:
- name: Check key and certificate
id: prechecks
uses: actions/github-script@v3
env:
title: ${{ github.event.issue.title }}
body: ${{ github.event.issue.body }}
certificate: ${{ secrets.SSH_CERTIFICATE }}
org: ${{ github.event.repository.owner.login }}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const { title, body, certificate, org } = process.env;
core.setOutput('org', org)
let recipient = title.match(/^Sign ssh key( for )?@?(.*)/)[2]
if(!recipient || recipient.trim() === '') {
recipient = context.actor
}
if (!certificate || certificate.trim() === '') {
message = '👋 @' + context.actor + ', seems as if this workflow was not setup with an SSH_CERTIFICATE [secret](https://docs.github.com/en/actions/reference/encrypted-secrets) yet - which is needed to [sign](https://docs.github.com/en/github/setting-up-and-managing-organizations-and-teams/about-ssh-certificate-authorities) the key of the user you provided.'
core.setOutput('error', message)
throw new Error(message)
}
try {
const recipientAccount = await github.users.getByUsername({username: recipient})
core.setOutput('recipient', recipientAccount.data.login)
} catch (Error) {
message = '👋 @' + context.actor + ', seems as if the GitHub user account you have specified in the issue title does not exist: `' + recipient + '`'
core.setOutput('error', message)
throw new Error(message)
}
keyIndex=0
try {
keyIndex=body.match(/^\/sign-ssh-key ([\d]+)/)[1]
} catch (Error) {
// parsing error, stick with first key
}
try {
const recipientKeys = await github.users.listPublicKeysForUser({username: recipient})
let key = recipientKeys.data[keyIndex].key
core.setOutput('key', key)
let algo = key.match(/^ssh-([\w]*)/)[1]
core.setOutput('algo', algo)
} catch (Error) {
message = '👋 @' + context.actor + ', seems as if @' + recipient + ' [has not registered](https://docs.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account) any key that could be signed, or the specified key index (' + keyIndex + ') was out of range.'
core.setOutput('error', message)
throw new Error(message)
}
- name: Pre-Check-Failed
id: precheck-failed
if: failure()
uses: actions/github-script@v3
env:
message: ${{steps.prechecks.outputs.error}}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const { message } = process.env;
github.issues.createComment({
...context.repo,
issue_number: context.issue.number,
body: message
})
github.issues.update({
...context.repo,
issue_number: context.issue.number,
state: 'closed'
})
act-on-issue:
runs-on: ubuntu-latest
needs: [prechecks]
steps:
- name: Signing request received
uses: actions/github-script@v3
env:
recipient: ${{needs.prechecks.outputs.recipient}}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const { recipient } = process.env;
github.issues.createComment({
...context.repo,
issue_number: context.issue.number,
body: '👋 @' + context.actor + ', starting to sign the [public ssh key](https://github.com/' + recipient + '.keys) by your [certificate authority](https://docs.github.com/en/github/setting-up-and-managing-organizations-and-teams/about-ssh-certificate-authorities) of user @' + recipient + ' ...'
})
- name: Checkout
uses: actions/checkout@v2
- name: Signing ssh key
id: sign
run: |
./sign-ssh-key.sh "$RECIPIENT" "id_${ALGO}_${RECIPIENT}_${ORG}"
echo "::set-output name=sign::"`cat id_${ALGO}_${RECIPIENT}_${ORG}-cert.pub | base64 -w 0`
echo
env:
SSH_CERTIFICATE: ${{ secrets.SSH_CERTIFICATE }}
RECIPIENT: ${{needs.prechecks.outputs.recipient}}
KEY: ${{needs.prechecks.outputs.key}}
ORG: ${{needs.prechecks.outputs.org}}
ALGO: ${{needs.prechecks.outputs.algo}}
- name: Failure
if: (failure())
uses: actions/github-script@v3
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
let log_url = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/` + process.env.GITHUB_RUN_ID
github.issues.createComment({
...context.repo,
issue_number: context.issue.number,
body: ':red_circle: Key signing attempt failed :cry:. [View details in the logs](' + log_url + ').'
})
- name: Provide key to the user
uses: actions/github-script@v3
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const commentBody = `\
## :key: New signed SSH key :key:
Run the following command to show your signed key:
\`\`\`bash
echo '${{steps.sign.outputs.sign}}' | base64 -D
\`\`\`
Run the following command to install your signed key as default key (may override existing key):
\`\`\`bash
echo '${{steps.sign.outputs.sign}}' | base64 -D > $HOME/.ssh/id_${{needs.prechecks.outputs.algo}}-cert.pub
\`\`\`
`;
await github.issues.createComment({
...context.repo,
issue_number: context.issue.number,
body: commentBody
})
github.issues.update({
...context.repo,
issue_number: context.issue.number,
state: 'closed'
})