-
Notifications
You must be signed in to change notification settings - Fork 282
/
authn.go
380 lines (342 loc) · 12.5 KB
/
authn.go
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//
// Copyright 2020 Verizon Media
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package sia
import (
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"github.com/AthenZ/athenz/clients/go/zts"
"github.com/AthenZ/athenz/provider/azure/sia-vm/data/attestation"
"github.com/AthenZ/athenz/provider/azure/sia-vm/logutil"
"github.com/AthenZ/athenz/provider/azure/sia-vm/options"
"github.com/AthenZ/athenz/provider/azure/sia-vm/util"
"io"
"io/ioutil"
"os/exec"
"strings"
"time"
)
const sshPubKeyFile = "/etc/ssh/ssh_host_rsa_key.pub"
const sshCertFile = "/etc/ssh/ssh_host_rsa_key-cert.pub"
const sshConfigFile = "/etc/ssh/sshd_config"
type Identity struct {
Name string
InstanceId string
Ip string
}
func GetRoleCertificate(ztsUrl, svcKeyFile, svcCertFile string, opts *options.Options, sysLogger io.Writer) bool {
client, err := util.ZtsClient(ztsUrl, opts.ZTSServerName, svcKeyFile, svcCertFile, opts.ZTSCACertFile, sysLogger)
if err != nil {
logutil.LogInfo(sysLogger, "unable to initialize ZTS Client for %s, err: %v\n", ztsUrl, err)
return false
}
client.AddCredentials("User-Agent", opts.Version)
// extract the provider from the certificate file
provider := extractProviderFromCert(svcCertFile)
key, err := util.PrivateKeyFromFile(svcKeyFile)
if err != nil {
logutil.LogInfo(sysLogger, "unable to read private key from %s, err: %v\n", svcKeyFile, err)
return false
}
// initialize our return state to success
failures := 0
var roleRequest = new(zts.RoleCertificateRequest)
for roleName, role := range opts.Roles {
domainNameRequest, roleNameRequest, err := util.SplitRoleName(roleName)
if err != nil {
logutil.LogInfo(sysLogger, "invalid role name: %s, err: %v\n", roleName, err)
failures += 1
continue
}
certFilePem := util.GetRoleCertFileName(mkDirPath(opts.CertDir), role.Filename, roleName)
spiffe := fmt.Sprintf("spiffe://%s/ra/%s", domainNameRequest, roleNameRequest)
csr, err := util.GenerateCSR(key, opts.CountryName, opts.Domain, opts.Services[0].Name, roleName, "", provider, spiffe, opts.ZTSAzureDomain, true)
if err != nil {
logutil.LogInfo(sysLogger, "unable to generate CSR for %s, err: %v\n", roleName, err)
failures += 1
continue
}
roleRequest.Csr = csr
// "rolename": "athenz.fp:role.readers"
// from the rolename, domain is athenz.fp and role is readers
roleToken, err := client.PostRoleCertificateRequest(zts.DomainName(domainNameRequest), zts.EntityName(roleNameRequest), roleRequest)
if err != nil {
logutil.LogInfo(sysLogger, "PostRoleCertificateRequest failed for %s, err: %v\n", roleName, err)
failures += 1
continue
}
// we have the role certificate
// write the cert to pem file using Role.Filename
err = util.UpdateFile(certFilePem, roleToken.Token, 0, 0, 0444, sysLogger)
if err != nil {
failures += 1
continue
}
}
logutil.LogInfo(sysLogger, "SIA processed %d (failures %d) role certificate requests\n", len(opts.Roles), failures)
return failures == 0
}
func RegisterInstance(data []*attestation.Data, ztsUrl string, identityDocument *attestation.IdentityDocument, opts *options.Options, sysLogger io.Writer) error {
for i, svc := range opts.Services {
err := registerSvc(svc, data[i], ztsUrl, identityDocument, opts, sysLogger)
if err != nil {
return fmt.Errorf("unable to register identity for svc: %q, error: %v", svc.Name, err)
}
}
return nil
}
func registerSvc(svc options.Service, data *attestation.Data, ztsUrl string, identityDocument *attestation.IdentityDocument, opts *options.Options, sysLogger io.Writer) error {
key, err := util.GenerateKeyPair(2048)
if err != nil {
return err
}
// include a csr for the host ssh certificate if requested
ssh, err := generateSSHHostCSR(opts.Ssh, opts.Domain, svc.Name, identityDocument.PrivateIp, opts.ZTSAzureDomain, sysLogger)
if err != nil {
return err
}
provider := getProviderName(opts.Provider, identityDocument.Location)
spiffe := fmt.Sprintf("spiffe://%s/sa/%s", opts.Domain, svc.Name)
commonName := fmt.Sprintf("%s.%s", opts.Domain, svc.Name)
csr, err := util.GenerateCSR(key, opts.CountryName, opts.Domain, svc.Name, commonName, identityDocument.VmId, provider, spiffe, opts.ZTSAzureDomain, false)
if err != nil {
return err
}
var info zts.InstanceRegisterInformation
info.Provider = zts.ServiceName(provider)
info.Domain = zts.DomainName(opts.Domain)
info.Service = zts.SimpleName(svc.Name)
info.Csr = csr
info.Ssh = ssh
attestData, err := json.Marshal(data)
if err != nil {
return err
}
info.AttestationData = string(attestData)
client, err := util.ZtsClient(ztsUrl, opts.ZTSServerName, "", "", opts.ZTSCACertFile, sysLogger)
if err != nil {
return err
}
client.AddCredentials("User-Agent", opts.Version)
instIdent, _, err := client.PostInstanceRegisterInformation(&info)
if err != nil {
logutil.LogInfo(sysLogger, "Unable to do PostInstanceRegisterInformation, err: %v\n", err)
return err
}
svcKeyFile := fmt.Sprintf("%s/%s.%s.key.pem", opts.KeyDir, opts.Domain, svc.Name)
err = util.UpdateFile(svcKeyFile, util.PrivatePem(key), svc.Uid, svc.Gid, 0440, sysLogger)
if err != nil {
return err
}
certFile := getCertFileName(svc.Filename, opts.Domain, svc.Name, opts.CertDir)
err = util.UpdateFile(certFile, instIdent.X509Certificate, svc.Uid, svc.Gid, 0444, sysLogger)
if err != nil {
return err
}
if opts.Services[0].Name == svc.Name {
err = util.UpdateFile(opts.AthenzCACertFile, instIdent.X509CertificateSigner, svc.Uid, svc.Gid, 0444, sysLogger)
if err != nil {
return err
}
// we're not going to count ssh updates as fatal since the primary
// task for sia to get service identity certs but we'll log the failure
err = updateSSH(instIdent.SshCertificate, instIdent.SshCertificateSigner, sysLogger)
if err != nil {
logutil.LogInfo(sysLogger, "Unable to update ssh certificate, err: %v\n", err)
}
}
return nil
}
func RefreshInstance(data []*attestation.Data, ztsUrl string, identityDocument *attestation.IdentityDocument, opts *options.Options, sysLogger io.Writer) error {
for i, svc := range opts.Services {
err := refreshSvc(svc, data[i], ztsUrl, identityDocument, opts, sysLogger)
if err != nil {
return fmt.Errorf("unable to refresh identity for svc: %q, error: %v", svc.Name, err)
}
}
return nil
}
func refreshSvc(svc options.Service, data *attestation.Data, ztsUrl string, identityDocument *attestation.IdentityDocument, opts *options.Options, sysLogger io.Writer) error {
keyFile := fmt.Sprintf("%s/%s.%s.key.pem", opts.KeyDir, opts.Domain, svc.Name)
key, err := util.PrivateKeyFromFile(keyFile)
if err != nil {
logutil.LogInfo(sysLogger, "Unable to read private key from %s, err: %v\n", keyFile, err)
return err
}
certFile := fmt.Sprintf("%s/%s.%s.cert.pem", opts.CertDir, opts.Domain, svc.Name)
// include a csr for the host ssh certificate if requested
ssh, err := generateSSHHostCSR(opts.Ssh, opts.Domain, svc.Name, identityDocument.PrivateIp, opts.ZTSAzureDomain, sysLogger)
if err != nil {
return err
}
attestData, err := json.Marshal(data)
if err != nil {
return err
}
provider := getProviderName(opts.Provider, identityDocument.Location)
spiffe := fmt.Sprintf("spiffe://%s/sa/%s", opts.Domain, svc.Name)
commonName := fmt.Sprintf("%s.%s", opts.Domain, svc.Name)
csr, err := util.GenerateCSR(key, opts.CountryName, opts.Domain, svc.Name, commonName, identityDocument.VmId, provider, spiffe, opts.ZTSAzureDomain, false)
if err != nil {
logutil.LogInfo(sysLogger, "Unable to generate CSR for %s, err: %v\n", opts.Name, err)
return err
}
info := &zts.InstanceRefreshInformation{AttestationData: string(attestData), Csr: csr, Ssh: ssh}
infoData, err := json.Marshal(info)
logutil.LogInfo(sysLogger, "attestation data: \n%s\n", infoData)
client, err := util.ZtsClient(ztsUrl, opts.ZTSServerName, keyFile, certFile, opts.ZTSCACertFile, sysLogger)
if err != nil {
logutil.LogInfo(sysLogger, "Unable to get ZTS Client for %s, err: %v\n", ztsUrl, err)
return err
}
client.AddCredentials("User-Agent", opts.Version)
ident, err := client.PostInstanceRefreshInformation(zts.ServiceName(provider), zts.DomainName(opts.Domain), zts.SimpleName(svc.Name), zts.PathElement(identityDocument.VmId), info)
if err != nil {
logutil.LogInfo(sysLogger, "Unable to refresh instance service certificate for %s, err: %v\n", opts.Name, err)
return err
}
err = util.UpdateFile(certFile, ident.X509Certificate, svc.Uid, svc.Gid, 0444, sysLogger)
if err != nil {
return err
}
if opts.Services[0].Name == svc.Name {
err = util.UpdateFile(opts.AthenzCACertFile, ident.X509CertificateSigner, svc.Uid, svc.Gid, 0444, sysLogger)
if err != nil {
return err
}
// we're not going to count ssh updates as fatal since the primary
// task for sia to get service identity certs but we'll log the failure
err = updateSSH(ident.SshCertificate, ident.SshCertificateSigner, sysLogger)
if err != nil {
logutil.LogInfo(sysLogger, "Unable to update ssh certificate, err: %v\n", err)
}
}
return nil
}
func updateSSH(hostCert, hostSigner string, sysLogger io.Writer) error {
// if we have no hostCert and hostSigner then
// we have nothing to update for ssh access
if hostCert == "" && hostSigner == "" {
logutil.LogInfo(sysLogger, "No host ssh certificate available to update\n")
return nil
}
// write the host cert file
err := util.UpdateFile(sshCertFile, hostCert, 0, 0, 0644, sysLogger)
if err != nil {
return err
}
// Now update the config file, if needed
data, err := ioutil.ReadFile(sshConfigFile)
if err != nil {
return err
}
conf := string(data)
i := strings.Index(conf, "#HostCertificate /etc/ssh/ssh_host_rsa_key-cert.pub")
if i >= 0 {
conf = conf[:i] + conf[i+1:]
err = util.UpdateFile(sshConfigFile, conf, 0, 0, 0644, sysLogger)
if err != nil {
return err
}
// and restart sshd to notice the changes.
return restartSshdService()
}
return nil
}
func restartSshdService() error {
return exec.Command("systemctl", "restart", "sshd").Run()
}
// SSHKeyReq
type SSHKeyReq struct {
Principals []string `json:"principals"`
Ips []string `json:"ips,omitempty" rdl:"optional"`
Pubkey string `json:"pubkey"`
Reqip string `json:"reqip"`
Requser string `json:"requser"`
Certtype string `json:"certtype"`
Transid string `json:"transid"`
Command string `json:"command,omitempty" rdl:"optional"`
}
func generateSSHHostCSR(sshCert bool, domain, service, ip, ZTSAzureDomain string, sysLogger io.Writer) (string, error) {
if !sshCert {
return "", nil
}
pubkey, err := ioutil.ReadFile(sshPubKeyFile)
if err != nil {
logutil.LogInfo(sysLogger, "Skipping SSH CSR Request - Unable to read SSH Public Key File: %v\n", err)
return "", nil
}
identity := domain + "." + service
transId := fmt.Sprintf("%x", time.Now().Unix())
hyphenDomain := strings.Replace(domain, ".", "-", -1)
host := fmt.Sprintf("%s.%s.%s", service, hyphenDomain, ZTSAzureDomain)
req := &SSHKeyReq{
Principals: []string{host},
Pubkey: string(pubkey),
Reqip: ip,
Requser: identity,
Certtype: "host",
Transid: transId,
}
csr, err := json.Marshal(req)
if err != nil {
return "", err
}
return string(csr), err
}
func getProviderName(provider, region string) string {
if provider != "" {
return provider
}
return "athenz.azure." + region
}
func extractProviderFromCert(certFile string) string {
data, err := ioutil.ReadFile(certFile)
if err != nil {
return ""
}
var block *pem.Block
block, _ = pem.Decode(data)
if block == nil {
return ""
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return ""
}
if len(cert.Subject.OrganizationalUnit) > 0 {
return cert.Subject.OrganizationalUnit[0]
}
return ""
}
func getCertFileName(file, domain, service, certDir string) string {
switch {
case file == "":
return fmt.Sprintf("%s/%s.%s.cert.pem", certDir, domain, service)
case file[0] == '/':
return file
default:
return fmt.Sprintf("%s/%s", certDir, file)
}
}
// mkDirPath appends "/" if missing at the end
func mkDirPath(dir string) string {
if !strings.HasSuffix(dir, "/") {
return fmt.Sprintf("%s/", dir)
}
return dir
}