This repository has been archived by the owner on Apr 27, 2024. It is now read-only.
forked from apache/pulsar-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
athenz.go
201 lines (175 loc) · 5.19 KB
/
athenz.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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 auth
import (
"crypto/tls"
"encoding/base64"
"errors"
"io/ioutil"
"net/http"
"regexp"
"strings"
"time"
zms "github.com/AthenZ/athenz/libs/go/zmssvctoken"
zts "github.com/AthenZ/athenz/libs/go/ztsroletoken"
)
const (
minExpire = 2 * time.Hour
maxExpire = 24 * time.Hour
AthenzRoleAuthHeader = "Athenz-Role-Auth"
)
type athenzAuthProvider struct {
providerDomain string
tenantDomain string
tenantService string
privateKey string
keyID string
principalHeader string
ztsURL string
tokenBuilder zms.TokenBuilder
roleToken zts.RoleToken
zmsNewTokenBuilder func(domain, name string, privateKeyPEM []byte, keyVersion string) (zms.TokenBuilder, error)
ztsNewRoleToken func(tok zms.Token, domain string, opts zts.RoleTokenOptions) zts.RoleToken
T http.RoundTripper
}
type privateKeyURI struct {
Scheme string
MediaTypeAndEncodingType string
Data string
Path string
}
func NewAuthenticationAthenzWithParams(params map[string]string) (Provider, error) {
return NewAuthenticationAthenz(
params["providerDomain"],
params["tenantDomain"],
params["tenantService"],
params["privateKey"],
params["keyId"],
params["principalHeader"],
params["ztsUrl"],
), nil
}
func NewAuthenticationAthenz(
providerDomain string,
tenantDomain string,
tenantService string,
privateKey string,
keyID string,
principalHeader string,
ztsURL string) Provider {
var fixedKeyID string
if keyID == "" {
fixedKeyID = "0"
} else {
fixedKeyID = keyID
}
ztsNewRoleToken := func(tok zms.Token, domain string, opts zts.RoleTokenOptions) zts.RoleToken {
return zts.RoleToken(zts.NewRoleToken(tok, domain, opts))
}
return &athenzAuthProvider{
providerDomain: providerDomain,
tenantDomain: tenantDomain,
tenantService: tenantService,
privateKey: privateKey,
keyID: fixedKeyID,
principalHeader: principalHeader,
ztsURL: strings.TrimSuffix(ztsURL, "/"),
zmsNewTokenBuilder: zms.NewTokenBuilder,
ztsNewRoleToken: ztsNewRoleToken,
}
}
func (p *athenzAuthProvider) Init() error {
uriSt := parseURI(p.privateKey)
var keyData []byte
if uriSt.Scheme == "data" {
if uriSt.MediaTypeAndEncodingType != "application/x-pem-file;base64" {
return errors.New("Unsupported mediaType or encodingType: " + uriSt.MediaTypeAndEncodingType)
}
key, err := base64.StdEncoding.DecodeString(uriSt.Data)
if err != nil {
return err
}
keyData = key
} else if uriSt.Scheme == "file" {
key, err := ioutil.ReadFile(uriSt.Path)
if err != nil {
return err
}
keyData = key
} else {
return errors.New("Unsupported URI Scheme: " + uriSt.Scheme)
}
tb, err := p.zmsNewTokenBuilder(p.tenantDomain, p.tenantService, keyData, p.keyID)
if err != nil {
return err
}
p.tokenBuilder = tb
roleToken := p.ztsNewRoleToken(p.tokenBuilder.Token(), p.providerDomain, zts.RoleTokenOptions{
BaseZTSURL: p.ztsURL + "/zts/v1",
MinExpire: minExpire,
MaxExpire: maxExpire,
AuthHeader: p.principalHeader,
})
p.roleToken = roleToken
return nil
}
func (p *athenzAuthProvider) Name() string {
return "athenz"
}
func (p *athenzAuthProvider) GetTLSCertificate() (*tls.Certificate, error) {
return nil, nil
}
func (p *athenzAuthProvider) GetData() ([]byte, error) {
tok, err := p.roleToken.RoleTokenValue()
if err != nil {
return nil, err
}
return []byte(tok), nil
}
func (p *athenzAuthProvider) Close() error {
return nil
}
func parseURI(uri string) privateKeyURI {
var uriSt privateKeyURI
// scheme mediatype[;base64] path file
const expression = `^(?:([^:/?#]+):)(?:([;/\\\-\w]*),)?(?:/{0,2}((?:[^?#/]*/)*))?([^?#]*)`
// when expression cannot be parsed, then panics
re := regexp.MustCompile(expression)
if re.MatchString(uri) {
groups := re.FindStringSubmatch(uri)
uriSt.Scheme = groups[1]
uriSt.MediaTypeAndEncodingType = groups[2]
uriSt.Data = groups[4]
uriSt.Path = groups[3] + groups[4]
}
return uriSt
}
func (p *athenzAuthProvider) RoundTrip(req *http.Request) (*http.Response, error) {
tok, err := p.roleToken.RoleTokenValue()
if err != nil {
return nil, err
}
req.Header.Add(AthenzRoleAuthHeader, tok)
return p.T.RoundTrip(req)
}
func (p *athenzAuthProvider) Transport() http.RoundTripper {
return p.T
}
func (p *athenzAuthProvider) WithTransport(tripper http.RoundTripper) error {
p.T = tripper
return nil
}