-
Notifications
You must be signed in to change notification settings - Fork 40
/
msi_stub.go
58 lines (48 loc) · 1.42 KB
/
msi_stub.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
package test
import (
"context"
"fmt"
"log"
"net/http"
)
func ManagedIdentityStubServer(ctx context.Context, port int, token string) chan bool {
handler := http.NewServeMux()
handler.HandleFunc("/metadata/identity/oauth2/token", func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
clientId := q.Get("client_id")
resource := q.Get("resource")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if r.Header.Get("Metadata") != "true" {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, `{}`)
return
}
fmt.Fprintf(w, `{"access_token":"%s","client_id":"%s","expires_in":"86391","expires_on":"1611701390","ext_expires_in":"86399","not_before":"1611614690","resource":"%s","token_type":"Bearer"}`,
token, clientId, resource)
})
handler.HandleFunc("/metadata", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprint(w, `
`)
})
done := make(chan bool, 1)
server := &http.Server{
Addr: fmt.Sprintf("127.0.0.1:%d", port),
Handler: handler,
}
go func() {
<-done
err := server.Shutdown(ctx)
if err != nil {
log.Fatalf("failed to gracefully shut down MSI stub server: %v", err)
}
}()
go func() {
log.Printf("MSI Stub Service listening on 127.0.0.1:%d\n", port)
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatalf("server.ListenAndServe: %v", err)
}
}()
return done
}