-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfn.go
More file actions
168 lines (149 loc) · 3.87 KB
/
Copy pathfn.go
File metadata and controls
168 lines (149 loc) · 3.87 KB
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
//
// Copyright (c) 2020 SSH Communications Security Inc.
//
// All rights reserved.
//
package gcloudwebhook
import (
"context"
"crypto/ed25519"
"fmt"
"log"
"net/http"
"os"
"cloud.google.com/go/firestore"
"github.com/SSHcom/privx-sdk-go/api"
"github.com/SSHcom/privx-sdk-go/api/rolestore"
"github.com/SSHcom/privx-sdk-go/oauth"
"github.com/markkurossi/cloudsdk/api/auth"
"github.com/markkurossi/go-libs/fn"
)
const (
// Realm specifies the HTTP authentication realm.
Realm = "Jira PrivX Webhook"
// Tenant specifies the OAuth2 authentication tenant.
Tenant = "Jira-PrivX-Webhook"
// InstanceEnvVar specifies the environment variable that holds
// the PrivX instance name.
InstanceEnvVar = "PRIVX_INSTANCE"
)
var (
mux *http.ServeMux
projectID string
store *auth.ClientStore
tenant *auth.Tenant
authPubkey ed25519.PublicKey
ctx context.Context
fs *firestore.Client
instance *Instance
roleStore *rolestore.Client
userIDs = make(map[string]string)
roleIDs = make(map[string]string)
)
func init() {
mux = http.NewServeMux()
mux.HandleFunc("/jira", jira)
id, err := fn.GetProjectID()
if err != nil {
log.Fatalf("fn.GetProjectID: %s", err)
}
projectID = id
store, err = auth.NewClientStore()
if err != nil {
log.Fatalf("NewClientStore: %s\n", err)
}
tenants, err := store.TenantByName(Tenant)
if err != nil {
log.Fatalf("store.TenantByName: %s\n", err)
}
if len(tenants) == 0 {
log.Fatalf("Tenant %s not found\n", Tenant)
}
tenant = tenants[0]
assets, err := store.Asset(auth.ASSET_AUTH_PUBKEY)
if err != nil {
log.Fatalf("store.Asset(%s)\n", auth.ASSET_AUTH_PUBKEY)
}
if len(assets) == 0 {
log.Fatalf("No auth public key\n")
}
authPubkey = ed25519.PublicKey(assets[0].Data)
ctx = context.Background()
fs, err = firestore.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("firestoer.NewClient: %s", err)
}
instanceName, ok := os.LookupEnv(InstanceEnvVar)
if !ok {
_, ok = os.LookupEnv("GCP_PROJECT")
if ok {
log.Fatalf("PrivX instance name not set: $%s", InstanceEnvVar)
} else {
log.Printf("Privx instance name not set: $%s", InstanceEnvVar)
}
} else {
instances, err := GetPrivXInstance(instanceName)
if err != nil {
log.Fatalf("GetPrivXInstances: %s", err)
}
if len(instances) != 1 {
log.Fatalf("Invalid amount (%d) of PrivX instances with name '%s'",
len(instances), instanceName)
}
instance = instances[0]
initPrivX()
}
}
func initPrivX() {
auth, err := oauth.NewClient(instance.Config.Auth,
instance.Config.API.Endpoint,
instance.Config.API.Certificate.X509, true)
if err != nil {
log.Fatal(err)
}
client, err := api.NewClient(api.Authenticator(auth),
api.Endpoint(instance.Config.API.Endpoint),
api.X509(instance.Config.API.Certificate.X509))
if err != nil {
log.Fatal(err)
}
roleStore, err = rolestore.NewClient(client)
if err != nil {
log.Fatal(err)
}
// Resolve user IDs
for k, v := range instance.UserMappings {
users, err := roleStore.SearchUsers(v, "")
if err != nil {
log.Fatalf("Searching user '%s' failed: %s", v, err)
}
switch len(users) {
case 0:
log.Printf("User '%s' not found", v)
case 1:
userIDs[k] = users[0].ID
default:
log.Printf("Multiple matches (%d) for user '%s'", len(users), v)
}
}
// Resolve role IDs.
roles, err := roleStore.GetRoles()
if err != nil {
log.Fatalf("Failed to get roles: %s", err)
}
for _, role := range roles {
roleIDs[role.Name] = role.ID
}
}
// PrivXWebhook is the cloud function entry point for Jira-PrivX
// integration.
func PrivXWebhook(w http.ResponseWriter, r *http.Request) {
mux.ServeHTTP(w, r)
}
func tokenVerifier(message, sig []byte) bool {
return ed25519.Verify(authPubkey, message, sig)
}
// Errorf formats an HTTP error response.
func Errorf(w http.ResponseWriter, code int, format string, a ...interface{}) {
http.Error(w, fmt.Sprintf(format, a...), code)
}