-
Notifications
You must be signed in to change notification settings - Fork 2
/
prinicpal_provider.go
71 lines (55 loc) · 1.91 KB
/
prinicpal_provider.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
package provider
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/ottogroup/penelope/pkg/config"
authmodel "github.com/ottogroup/penelope/pkg/http/auth/model"
"github.com/ottogroup/penelope/pkg/service/gcs"
"go.opencensus.io/trace"
"gopkg.in/yaml.v2"
)
type PrincipalProvider interface {
GetPrincipalForEmail(ctxIn context.Context, email string) (*authmodel.Principal, error)
}
type defaultUserProvider struct {
client gcs.CloudStorageClient
}
func NewDefaultUserProvider(ctxIn context.Context, gcsClient gcs.CloudStorageClient) (PrincipalProvider, error) {
ctx, span := trace.StartSpan(ctxIn, "NewDefaultGCPBackupProvider")
defer span.End()
if gcsClient == nil || !gcsClient.IsInitialized(ctx) {
return &defaultUserProvider{}, fmt.Errorf("can not create instance of defaultGCSBackupProvider with unititialized GcsClient")
}
return &defaultUserProvider{
client: gcsClient,
}, nil
}
func (p *defaultUserProvider) GetPrincipalForEmail(ctxIn context.Context, email string) (*authmodel.Principal, error) {
ctx, span := trace.StartSpan(ctxIn, "(*defaultUserProvider).GetSinkGCPProjectID")
defer span.End()
bucketName := config.DefaultProviderBucketEnv.MustGet()
objectName := config.DefaultProviderPrincipalForUserPathEnv.MustGet()
var object []byte
var err error
if config.IsProviderLocal.GetBoolOrDefault(false) {
filePath := filepath.Join(bucketName, objectName)
object, err = os.ReadFile(filePath)
} else {
object, err = p.client.ReadObject(ctx, bucketName, objectName)
}
if err != nil {
return nil, err
}
var principal []*authmodel.Principal
if err = yaml.Unmarshal(object, &principal); err != nil {
return nil, fmt.Errorf("can not parse yaml file %s", err)
}
for _, p := range principal {
if p.User.Email == email {
return p, nil
}
}
return nil, fmt.Errorf("could not find user '%s' in provided path %s", email, config.DefaultProviderPrincipalForUserPathEnv.MustGet())
}