-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext.go
64 lines (50 loc) · 1.28 KB
/
context.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
package header
import (
"context"
"github.com/google/uuid"
)
type CtxKey string
var knownValueNames = []CtxKey{
HeaderAsertoTenantID,
HeaderAsertoAccountID,
HeaderAsertoRequestID,
ValueMachineAccountConnectionID,
}
// If a call is authenticated with a machine account, these hold info about the connection and cert that resolved
// that machine account.
var (
ValueMachineAccountConnectionID = CtxKey("machine-account-connection-id")
ValueMachineAccountCertID = CtxKey("machine-account-cert-id")
)
func IsValidUUID(uid string) bool {
_, err := uuid.Parse(uid)
return err == nil
}
// KnownContextValueStrings is the same as KnownContextValues, but uses string keys (useful for logging).
func KnownContextValueStrings(ctx context.Context) map[string]interface{} {
result := map[string]interface{}{}
for _, k := range knownValueNames {
v := extract(ctx, k)
if v != "" {
result[string(k)] = v
}
}
return result
}
func KnownContextValues(ctx context.Context) map[interface{}]interface{} {
result := map[interface{}]interface{}{}
for _, k := range knownValueNames {
v := extract(ctx, k)
if v != "" {
result[k] = v
}
}
return result
}
func extract(ctx context.Context, key CtxKey) string {
id, ok := ctx.Value(key).(string)
if !ok {
return ""
}
return id
}