-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
30 lines (24 loc) · 824 Bytes
/
auth.go
File metadata and controls
30 lines (24 loc) · 824 Bytes
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
package cloudrunauth
import (
"context"
"fmt"
"cloud.google.com/go/compute/metadata"
)
type MetadataServerToken struct {
ServiceURL string
}
// GetRequestMetadata is called on every request, so we are sure that token is always not expired
func (t MetadataServerToken) GetRequestMetadata(ctx context.Context, in ...string) (map[string]string, error) {
// based on https://cloud.google.com/run/docs/authenticating/service-to-service#go
tokenURL := fmt.Sprintf("/instance/service-accounts/default/identity?audience=%s", t.ServiceURL)
idToken, err := metadata.Get(tokenURL)
if err != nil {
return nil, fmt.Errorf("cannot query id token for gRPC: %w", err)
}
return map[string]string{
"authorization": "Bearer " + idToken,
}, nil
}
func (MetadataServerToken) RequireTransportSecurity() bool {
return true
}