forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure_msi_middleware.go
187 lines (158 loc) · 4.97 KB
/
azure_msi_middleware.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
// Copyright 2022 Gravitational, Inc
//
// 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 alpnproxy
import (
"crypto"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
"github.com/sirupsen/logrus"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/jwt"
)
// AzureMSIMiddleware implements a simplified version of MSI server serving auth tokens.
type AzureMSIMiddleware struct {
// Identity is the Azure identity to be served by the server. Only single identity will be provided.
Identity string
// TenantID to be returned in a claim. Doesn't have to match actual TenantID as recognized by Azure.
TenantID string
// ClientID to be returned in a claim.
ClientID string
// Key used to sign JWT
Key crypto.Signer
// Clock is used to override time in tests.
Clock clockwork.Clock
// Log is the Logger.
Log logrus.FieldLogger
// Secret to be provided by the client.
Secret string
}
var _ LocalProxyHTTPMiddleware = &AzureMSIMiddleware{}
func (m *AzureMSIMiddleware) CheckAndSetDefaults() error {
if m.Clock == nil {
m.Clock = clockwork.NewRealClock()
}
if m.Log == nil {
m.Log = logrus.WithField(trace.Component, "azure_msi")
}
if m.Key == nil {
return trace.BadParameter("missing Key")
}
if m.Secret == "" {
return trace.BadParameter("missing Secret")
}
if m.Identity == "" {
return trace.BadParameter("missing Identity")
}
if m.TenantID == "" {
return trace.BadParameter("missing TenantID")
}
if m.ClientID == "" {
return trace.BadParameter("missing ClientID")
}
return nil
}
func (m *AzureMSIMiddleware) HandleRequest(rw http.ResponseWriter, req *http.Request) bool {
if req.Host == types.TeleportAzureMSIEndpoint {
if err := m.msiEndpoint(rw, req); err != nil {
m.Log.Warnf("Bad MSI request: %v", err)
trace.WriteError(rw, trace.Wrap(err))
}
return true
}
return false
}
func (m *AzureMSIMiddleware) msiEndpoint(rw http.ResponseWriter, req *http.Request) error {
// request validation
if req.URL.Path != ("/" + m.Secret) {
return trace.BadParameter("invalid secret")
}
metadata := req.Header.Get("Metadata")
if metadata != "true" {
return trace.BadParameter("expected Metadata header with value 'true'")
}
err := req.ParseForm()
if err != nil {
return trace.Wrap(err)
}
resource := req.Form.Get("resource")
if resource == "" {
return trace.BadParameter("missing value for parameter 'resource'")
}
// check that msi_res_id matches expected Azure Identity
requestedAzureIdentity := req.Form.Get("msi_res_id")
if requestedAzureIdentity != m.Identity {
m.Log.Warnf("Requested unexpected identity %q, expected %q", requestedAzureIdentity, m.Identity)
return trace.BadParameter("unexpected value for parameter 'msi_res_id': %v", requestedAzureIdentity)
}
respBody, err := m.fetchMSILoginResp(resource)
if err != nil {
return trace.Wrap(err)
}
m.Log.Infof("MSI: returning token for identity %v", m.Identity)
rw.Header().Add("Content-Type", "application/json; charset=utf-8")
rw.Header().Add("Content-Length", fmt.Sprintf("%v", len(respBody)))
rw.WriteHeader(200)
_, _ = rw.Write(respBody)
return nil
}
func (m *AzureMSIMiddleware) fetchMSILoginResp(resource string) ([]byte, error) {
now := m.Clock.Now()
notBefore := now.Add(-10 * time.Second)
expiresOn := now.Add(time.Hour * 24 * 365)
expiresIn := int64(expiresOn.Sub(now).Seconds())
accessToken, err := m.toJWT(jwt.AzureTokenClaims{
TenantID: m.TenantID,
Resource: resource,
})
if err != nil {
return nil, trace.Wrap(err)
}
response := map[string]any{
"access_token": accessToken,
"client_id": m.ClientID,
"not_before": notBefore.Unix(),
"expires_on": expiresOn.Unix(),
"expires_in": expiresIn,
"ext_expires_in": expiresIn,
"token_type": "Bearer",
"resource": resource,
}
out, err := json.MarshalIndent(response, "", " ")
if err != nil {
return nil, trace.Wrap(err)
}
return out, nil
}
func (m *AzureMSIMiddleware) toJWT(claims jwt.AzureTokenClaims) (string, error) {
// Create a new key that can sign and verify tokens.
key, err := jwt.New(&jwt.Config{
Clock: m.Clock,
PrivateKey: m.Key,
Algorithm: defaults.ApplicationTokenAlgorithm,
ClusterName: types.TeleportAzureMSIEndpoint, // todo get cluster name
})
if err != nil {
return "", trace.Wrap(err)
}
token, err := key.SignAzureToken(claims)
if err != nil {
return "", trace.Wrap(err)
}
return token, nil
}