-
Notifications
You must be signed in to change notification settings - Fork 0
/
selfid.go
66 lines (60 loc) · 1.73 KB
/
selfid.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
package fleet
import (
"context"
"crypto/sha256"
"crypto/x509"
"encoding/hex"
"errors"
"fmt"
"io"
"log"
"net/http"
"time"
)
func (a *Agent) performSelfIdentificationAttempt() error {
// let's try various methods to self-id, such as fetching google
if err := a.performGoogleSelfId(); err == nil {
return nil
} else {
log.Printf("[directory] self auth via google failed: %s", err)
}
return errors.New("self-identification was not successful")
}
func (a *Agent) performGoogleSelfId() error {
// attempt to google self-auth
// call url: http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity?audience=%23abcdefg&format=full
// with header: Metadata-Flavor: Google
key, err := a.getLocalKey()
if err != nil {
return err
}
pubBin, err := x509.MarshalPKIXPublicKey(key.Public())
if err != nil {
return err
}
pubHash := sha256.Sum256(pubBin)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
url := "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity?audience=%23" + hex.EncodeToString(pubHash[:]) + "&format=full"
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("HTTP query failed: %s", resp.Status)
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
// data is a jwt token on a single line, no linebreak
_, err = a.GetFile(a, "selfid/google/"+string(data))
// if file fetch succeeded, it means this instance has been connected to the appropriate host
// if not, it means failure
return err
}