-
Notifications
You must be signed in to change notification settings - Fork 276
/
authn.go
114 lines (95 loc) · 3.92 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
//
// Copyright The Athenz Authors
//
// 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/rsa"
"fmt"
"github.com/AthenZ/athenz/libs/go/sia/util"
"github.com/go-jose/go-jose/v4"
"github.com/go-jose/go-jose/v4/jwt"
"io"
"k8s.io/apimachinery/pkg/util/json"
"net/http"
"net/url"
"os"
"time"
)
func GetOIDCToken(ztsUrl string) (string, map[string]interface{}, error) {
requestUrl := os.Getenv("ACTIONS_ID_TOKEN_REQUEST_URL")
if requestUrl == "" {
return "", nil, fmt.Errorf("ACTIONS_ID_TOKEN_REQUEST_URL environment variable not set")
}
requestToken := os.Getenv("ACTIONS_ID_TOKEN_REQUEST_TOKEN")
if requestToken == "" {
return "", nil, fmt.Errorf("ACTIONS_ID_TOKEN_REQUEST_TOKEN environment variable not set")
}
// get the id token for the GitHub actions
// we're going to use this token to authenticate our request to the ZTS server
githubUrl := fmt.Sprintf("%s&audience=%s", requestUrl, ztsUrl)
req, err := http.NewRequest(http.MethodGet, githubUrl, nil)
if err != nil {
return "", nil, fmt.Errorf("unable to generate new http request: %v", err)
}
req.Header.Add("User-Agent", "actions/oidc-client")
req.Header.Add("Authorization", "Bearer "+requestToken)
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
return "", nil, fmt.Errorf("unable to execute http get request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", nil, fmt.Errorf("oidc token get status error: %d", resp.StatusCode)
}
contents, err := io.ReadAll(resp.Body)
if err != nil {
return "", nil, fmt.Errorf("uanble to read response body: %v", err)
}
var jsonData map[string]interface{}
err = json.Unmarshal(contents, &jsonData)
if err != nil {
return "", nil, fmt.Errorf("unable to parse oidc token response: %v", err)
}
signatureAlgorithms := []jose.SignatureAlgorithm{jose.RS256, jose.RS384, jose.RS512, jose.PS256, jose.PS384, jose.PS512, jose.ES256, jose.ES384, jose.ES512, jose.EdDSA}
oidcToken := jsonData["value"].(string)
tok, err := jwt.ParseSigned(oidcToken, signatureAlgorithms)
if err != nil {
return "", nil, fmt.Errorf("unable to parse oidc token: %v", err)
}
var claims map[string]interface{}
err = tok.UnsafeClaimsWithoutVerification(&claims)
if err != nil {
return "", nil, fmt.Errorf("unable to extract oidc token claims: %v", err)
}
return oidcToken, claims, nil
}
func GetCSRDetails(privateKey *rsa.PrivateKey, domain, service, provider, instanceId, dnsDomain, spiffeTrustDomain, subjC, subjO, subjOU string) (string, error) {
// note: RFC 6125 states that if the SAN (Subject Alternative Name) exists,
// it is used, not the CA. So, we will always put the Athenz name in the CN
// (it is *not* a DNS domain name), and put the host name into the SAN.
var csrDetails util.CertReqDetails
csrDetails.CommonName = fmt.Sprintf("%s.%s", domain, service)
csrDetails.Country = subjC
csrDetails.OrgUnit = subjOU
csrDetails.Org = subjO
csrDetails.HostList = []string{}
csrDetails.HostList = append(csrDetails.HostList, util.SanDNSHostname(domain, service, dnsDomain))
// add our uri fields. spiffe uri must be the first entry
csrDetails.URIs = []*url.URL{}
csrDetails.URIs = util.AppendUri(csrDetails.URIs, util.GetSvcSpiffeUri(spiffeTrustDomain, "default", domain, service))
csrDetails.URIs = util.AppendUri(csrDetails.URIs, util.SanURIInstanceId(provider, instanceId))
return util.GenerateX509CSR(privateKey, csrDetails)
}