This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 300
/
main.go
96 lines (82 loc) · 3.43 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
// Copyright 2020 the Exposure Notifications Server 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.
// This package is a CLI tool for creating FederationAuthorization entries, controlling who can access the federationout endpoint.
// If the subject/issuer match an existing record, that record will be updated.
package main
import (
"context"
"flag"
"log"
"github.com/google/exposure-notifications-server/internal/federationin"
"github.com/google/exposure-notifications-server/internal/federationin/model"
"github.com/google/exposure-notifications-server/internal/federationout/database"
cflag "github.com/google/exposure-notifications-server/internal/flag"
"github.com/google/exposure-notifications-server/internal/setup"
coredb "github.com/google/exposure-notifications-server/pkg/database"
)
const (
defaultIssuer = "https://accounts.google.com"
)
var (
testRegions = []string{"TEST", "PROBE"}
subject = flag.String("subject", "", "(Required) The OIDC subject (for issuer https://accounts.google.com, this is the obfuscated Gaia ID.)")
audience = flag.String("audience", federationin.DefaultAudience, "The OIDC audience; leaving this blank will cause server to not enforce the audience claim.")
note = flag.String("note", "", "An open text note to include on the record.")
)
func main() {
var includeRegions, excludeRegions cflag.RegionListVar
flag.Var(&includeRegions, "regions", "A comma-separated list of regions to query. Leave blank for all regions.")
flag.Var(&excludeRegions, "exclude-regions", "A comma-separated list fo regions to exclude from the query.")
flag.Parse()
if *subject == "" {
log.Fatalf("--subject is required")
}
// Issue warnings about missing test regions in excludeRegions.
var missingTestRegions []string
for _, testRegion := range testRegions {
inExcluded := false
for _, excludedRegion := range excludeRegions {
if excludedRegion == testRegion {
inExcluded = true
break
}
}
if !inExcluded {
missingTestRegions = append(missingTestRegions, testRegion)
}
}
if len(missingTestRegions) > 0 {
log.Printf("\n\nWARNING: This record does not exclude test regions %q and is only appropriate for a test federation authorization.\n\n", missingTestRegions)
}
ctx := context.Background()
var config coredb.Config
env, err := setup.Setup(ctx, &config)
if err != nil {
log.Fatalf("failed to setup: %v", err)
}
defer env.Close(ctx)
db := database.New(env.Database())
auth := &model.FederationOutAuthorization{
Issuer: defaultIssuer, // Authorization interceptor currently only supports defaultIssuer.
Subject: *subject,
Audience: *audience,
Note: *note,
IncludeRegions: includeRegions,
ExcludeRegions: excludeRegions,
}
if err := db.AddFederationOutAuthorization(ctx, auth); err != nil {
log.Fatalf("adding new federation client authorization %#v: %v", auth, err)
}
log.Printf("Successfully added federation client authorization %#v", auth)
}