- Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathplatform.go
155 lines (123 loc) · 3.79 KB
/
platform.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
/*
2019 © Postgres.ai
*/
// Package platform provides a Platform service.
package platform
import (
"context"
"errors"
"fmt"
"net/url"
"gitlab.com/postgres-ai/database-lab/v3/pkg/client/platform"
"gitlab.com/postgres-ai/database-lab/v3/pkg/log"
)
// PersonalTokenVerifier declares an interface of a struct for Platform Personal Token verification.
typePersonalTokenVerifierinterface {
IsAllowedToken(ctx context.Context, tokenstring) bool
IsPersonalTokenEnabled() bool
}
// Config provides configuration for the Platform service.
typeConfigstruct {
URLstring`yaml:"url"`
OrgKeystring`yaml:"orgKey"`
ProjectNamestring`yaml:"projectName"`
AccessTokenstring`yaml:"accessToken"`
EnablePersonalTokenbool`yaml:"enablePersonalTokens"`
EnableTelemetrybool`yaml:"enableTelemetry"`
}
// Service defines a Platform service.
typeServicestruct {
Client*platform.Client
cfgConfig
tokenToken
}
// Token defines verified Platform Token.
typeTokenstruct {
OrganizationIDuint
}
// New creates a new platform service.
funcNew(ctx context.Context, cfgConfig, instanceIDstring) (*Service, error) {
s:=&Service{cfg: cfg}
client, err:=platform.NewClient(platform.ClientConfig{
URL: s.cfg.URL,
OrgKey: s.cfg.OrgKey,
ProjectName: s.cfg.ProjectName,
AccessToken: s.cfg.AccessToken,
InstanceID: instanceID,
})
iferr!=nil {
varcvWarning*platform.ConfigValidationWarning
iferrors.As(err, &cvWarning) {
log.Warn(err)
s.Client=client
returns, nil
}
returnnil, fmt.Errorf("failed to create new Platform Client: %w", err)
}
s.Client=client
ifs.cfg.AccessToken!="" {
platformToken, err:=client.CheckPlatformToken(ctx, platform.TokenCheckRequest{Token: s.cfg.AccessToken})
iferr!=nil {
returnnil, err
}
ifplatformToken.OrganizationID==0 {
returnnil, errors.New("invalid organization ID associated with the given Platform Access Token")
}
s.token=Token{
OrganizationID: platformToken.OrganizationID,
}
}
returns, nil
}
// Reload reloads service configuration.
func (s*Service) Reload(newService*Service) {
*s=*newService
}
// IsAllowedToken checks if the Platform Personal Token is allowed.
func (s*Service) IsAllowedToken(ctx context.Context, personalTokenstring) bool {
if!s.IsPersonalTokenEnabled() {
returnfalse
}
platformToken, err:=s.Client.CheckPlatformToken(ctx, platform.TokenCheckRequest{Token: personalToken})
iferr!=nil {
returnfalse
}
if!platformToken.Personal {
log.Dbg("Non-personal token given")
returnfalse
}
returns.isAllowedOrganization(platformToken.OrganizationID)
}
// IsPersonalTokenEnabled checks if the Platform Personal Token is enabled.
func (s*Service) IsPersonalTokenEnabled() bool {
returns.cfg.EnablePersonalToken
}
// isAllowedOrganization checks if organization is associated to the current Platform service.
func (s*Service) isAllowedOrganization(organizationIDuint) bool {
returnorganizationID!=0&&organizationID==s.token.OrganizationID
}
// IsTelemetryEnabled checks if the Platform Telemetry is enabled.
func (s*Service) IsTelemetryEnabled() bool {
returns.cfg.EnableTelemetry
}
// OriginURL reports the origin Platform hostname.
func (s*Service) OriginURL() string {
parsedURL, err:=url.Parse(s.cfg.URL)
iferr!=nil {
log.Dbg("Cannot parse Platform URL")
}
platformURL:= url.URL{Scheme: parsedURL.Scheme, Host: parsedURL.Host}
returnplatformURL.String()
}
// AccessToken returns Platform AccessToken.
func (s*Service) AccessToken() string {
returns.cfg.AccessToken
}
// Token returns verified Platform Token.
func (s*Service) Token() Token {
returns.token
}
// OrgKey returns the organization key of the instance.
func (s*Service) OrgKey() string {
returns.cfg.OrgKey
}