This repository was archived by the owner on Jun 16, 2025. It is now read-only.
forked from uswitch/kiam
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandler_role_name.go
More file actions
122 lines (102 loc) · 3.16 KB
/
handler_role_name.go
File metadata and controls
122 lines (102 loc) · 3.16 KB
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
// Copyright 2017 uSwitch
//
// 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 metadata
import (
"context"
"fmt"
"github.com/cenkalti/backoff"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"github.com/uswitch/kiam/pkg/server"
"github.com/uswitch/kiam/pkg/statsd"
"net/http"
"net/url"
"time"
)
type roleHandler struct {
client server.Client
getClientIP clientIPFunc
}
func trailingSlashSuffixRedirectHandler(rw http.ResponseWriter, req *http.Request) {
u, err := url.Parse(req.URL.String())
if err != nil {
log.Errorf("error parsing uri: %s", err)
http.Error(rw, "error parsing uri", http.StatusInternalServerError)
return
}
u.Path = fmt.Sprintf("%s/", u.Path)
http.Redirect(rw, req, u.String(), http.StatusMovedPermanently)
}
func (h *roleHandler) Install(router *mux.Router) {
handler := adapt(withMeter("roleName", h))
router.Handle("/{version}/meta-data/iam/security-credentials/", handler)
router.HandleFunc("/{version}/meta-data/iam/security-credentials", trailingSlashSuffixRedirectHandler)
}
func (h *roleHandler) Handle(ctx context.Context, w http.ResponseWriter, req *http.Request) (int, error) {
timer := prometheus.NewTimer(handlerTimer.WithLabelValues("roleName"))
defer timer.ObserveDuration()
if statsd.Enabled {
defer statsd.Client.NewTiming().Send("handler.role_name")
}
err := req.ParseForm()
if err != nil {
return http.StatusInternalServerError, err
}
ip, err := h.getClientIP(req)
if err != nil {
return http.StatusInternalServerError, err
}
role, err := findRole(ctx, h.client, ip)
if err != nil {
findRoleError.WithLabelValues("roleName").Inc()
return http.StatusInternalServerError, err
}
if role == "" {
emptyRole.WithLabelValues("roleName").Inc()
return http.StatusNotFound, EmptyRoleError
}
fmt.Fprint(w, role)
success.WithLabelValues("roleName").Inc()
return http.StatusOK, nil
}
const (
retryInterval = time.Millisecond * 5
)
func findRole(ctx context.Context, client server.Client, ip string) (string, error) {
logger := log.WithField("pod.ip", ip)
roleCh := make(chan string, 1)
op := func() error {
role, err := client.GetRole(ctx, ip)
if err != nil {
logger.Warnf("error finding role for pod: %s", err.Error())
return err
}
roleCh <- role
return nil
}
strategy := backoff.NewExponentialBackOff()
strategy.InitialInterval = retryInterval
err := backoff.Retry(op, backoff.WithContext(strategy, ctx))
if err != nil {
return "", err
}
return <-roleCh, nil
}
func newRoleHandler(client server.Client, getClientIP clientIPFunc) *roleHandler {
return &roleHandler{
client: client,
getClientIP: getClientIP,
}
}