-
Notifications
You must be signed in to change notification settings - Fork 59
/
main.go
169 lines (151 loc) · 5.09 KB
/
main.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2022 The Cloud Robotics Authors
//
// 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 main
import (
"context"
"flag"
"fmt"
"log/slog"
"net/http"
"os"
"path"
"strings"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"github.com/googlecloudrobotics/core/src/go/cmd/token-vendor/api"
apiv1 "github.com/googlecloudrobotics/core/src/go/cmd/token-vendor/api/v1"
"github.com/googlecloudrobotics/core/src/go/cmd/token-vendor/app"
"github.com/googlecloudrobotics/core/src/go/cmd/token-vendor/oauth"
"github.com/googlecloudrobotics/core/src/go/cmd/token-vendor/repository/k8s"
"github.com/googlecloudrobotics/core/src/go/cmd/token-vendor/repository/memory"
"github.com/googlecloudrobotics/core/src/go/cmd/token-vendor/tokensource"
"github.com/googlecloudrobotics/ilog"
)
type scopeFlags []string
func (i *scopeFlags) String() string {
return strings.Join(*i, ",")
}
func (i *scopeFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
type KeyStoreOpt string
const (
Kubernetes = "KUBERNETES"
Memory = "IN_MEMORY"
)
// Supported public key backends.
var keyStoreOpts = []string{string(Kubernetes), string(Memory)}
var (
verbose = flag.Bool("verbose", false, "Increase log level to DEBUG.")
// Backend options
keyStore = flag.String(
"key-store",
string(Kubernetes),
"Public key repository implementation to use. Options: "+strings.Join(keyStoreOpts, ","))
k8sQPS = flag.Int("k8s-qps", 25, "Limit of QPS to the Kubernetes API server.")
k8sBurst = flag.Int("k8s-burst", 50, "Burst limit of QPS to the Kubernetes API server.")
// API options
bind = flag.String("bind", "0.0.0.0", "Address to bind to")
port = flag.Int("port", 9090, "Port number to listen on")
basePath = flag.String("base",
"/apis/core.token-vendor",
"Base path where the API will be mounted to.")
// GCP Cloud options
project = flag.String("project", "", "The cloud project")
// Kubernetes backend options
namespace = flag.String("namespace", "default",
"The namespace where to store the device keys. (Kubernetes)")
// Authentication / JWT options
acceptedAudience = flag.String("accepted_audience",
"", "Endpoint URL of the token vendor. Used for verification of JWTs send by robots.")
scopes = scopeFlags{}
robotName = flag.String("service_account", "robot-service",
"Name of the service account to generate cloud access tokens for.")
)
func main() {
flag.Var(&scopes, "scope", "GCP scopes included in the token given out to robots.")
flag.Parse()
level := slog.LevelInfo
if *verbose {
level = slog.LevelDebug
}
logHandler := ilog.NewLogHandler(level, os.Stderr)
slog.SetDefault(slog.New(logHandler))
// init components
ctx := context.Background()
var rep app.PubKeyRepository
var err error
if *keyStore == Kubernetes {
config, err := rest.InClusterConfig()
if err != nil {
slog.Error("Failed to get config", ilog.Err(err))
os.Exit(1)
}
config.QPS = float32(*k8sQPS)
config.Burst = *k8sBurst
cs, err := kubernetes.NewForConfig(config)
if err != nil {
slog.Error("Failed to make clientset", ilog.Err(err))
os.Exit(1)
}
rep, err = k8s.NewK8sRepository(ctx, cs, *namespace)
if err != nil {
slog.Error("Failed to make k8s repository client", ilog.Err(err))
os.Exit(1)
}
} else if *keyStore == Memory {
rep, err = memory.NewMemoryRepository(ctx)
if err != nil {
slog.Error("Failed to make in-memory repository", ilog.Err(err))
os.Exit(1)
}
} else {
slog.Error("unsupported key store option", slog.String("Value", *keyStore))
os.Exit(1)
}
slog.Info("Set up key store", slog.String("KeyStore", *keyStore))
verifier, err := oauth.NewTokenVerifier(ctx, &http.Client{}, *project)
if err != nil {
slog.Error("Failed to make verifier", ilog.Err(err))
os.Exit(1)
}
ts, err := tokensource.NewGCPTokenSource(ctx, nil, *project, *robotName, scopes)
if err != nil {
slog.Error("Failed to make token source", ilog.Err(err))
os.Exit(1)
}
tv, err := app.NewTokenVendor(ctx, rep, verifier, ts, *acceptedAudience)
if err != nil {
slog.Error("Failed to make token vendor", ilog.Err(err))
os.Exit(1)
}
// register API endpoints
if err := api.Register(); err != nil {
slog.Error("Failed to register root endpoints", ilog.Err(err))
os.Exit(1)
}
if err := apiv1.Register(tv, path.Join(*basePath, "v1")); err != nil {
slog.Error("Failed to register v1 endpoints", ilog.Err(err))
os.Exit(1)
}
// serve API
addr := fmt.Sprintf("%s:%d", *bind, *port)
err = api.SetupAndServe(addr)
if err != nil {
slog.Error("Failed to listen", slog.String("IP", addr), ilog.Err(err))
os.Exit(1)
}
}