-
Notifications
You must be signed in to change notification settings - Fork 8
/
localsecrets.go
170 lines (138 loc) · 4.65 KB
/
localsecrets.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
170
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package localsecrets
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"sync"
"namespacelabs.dev/foundation/framework/secrets"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/keys"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/pkggraph"
)
type localSecrets struct {
keyDir fnfs.LocalFS
workspaceModule string
workspaceFS fs.FS
env *schema.Environment
mu sync.Mutex
cache map[string]*Bundle
}
type SecretsContext interface {
Workspace() cfg.Workspace
Environment() *schema.Environment
}
func NewLocalSecrets(env SecretsContext) (secrets.SecretsSource, error) {
keyDir, err := keys.KeysDir()
if err != nil {
if errors.Is(err, keys.ErrKeyGen) {
keyDir = nil
} else {
return nil, err
}
}
return &localSecrets{
keyDir: keyDir,
workspaceModule: env.Workspace().ModuleName(),
workspaceFS: env.Workspace().ReadOnlyFS(),
env: env.Environment(),
cache: map[string]*Bundle{},
}, nil
}
func (l *localSecrets) Load(ctx context.Context, modules pkggraph.ModuleResolver, req *secrets.SecretLoadRequest) (*schema.SecretResult, error) {
// Ordered by lookup order.
var bundles []*Bundle
if userSecrets, err := l.loadSecretsFor(ctx, modules, l.workspaceModule, UserBundleName); err != nil {
return nil, err
} else if userSecrets != nil {
bundles = append(bundles, userSecrets)
}
lookup := []string{l.workspaceModule}
if req.Server != nil && req.Server.ModuleName != "" {
if serverSecrets, err := l.loadSecretsFor(ctx, modules, req.Server.ModuleName, filepath.Join(req.Server.RelPath, ServerBundleName)); err != nil {
return nil, err
} else if serverSecrets != nil {
bundles = append(bundles, serverSecrets)
}
lookup = append(lookup, req.Server.ModuleName)
}
for _, moduleName := range lookup {
if bundle, err := l.loadSecretsFor(ctx, modules, moduleName, WorkspaceBundleName); err != nil {
return nil, err
} else if bundle != nil {
bundles = append(bundles, bundle)
}
}
return lookupSecret(ctx, l.env, req.SecretRef, bundles...)
}
func (l *localSecrets) MissingError(missing *schema.PackageRef, missingSpec *schema.SecretSpec, missingServer schema.PackageName) error {
label := fmt.Sprintf("\n # Description: %s\n # Server: %s\n ns secrets set --secret %s", missingSpec.Description, missingServer, missing.Canonical())
return fnerrors.UsageError(
fmt.Sprintf("Please run:\n%s", label),
"There are secrets required which have not been specified")
}
func (l *localSecrets) loadSecretsFor(ctx context.Context, modules pkggraph.ModuleResolver, moduleName, secretFile string) (*Bundle, error) {
if strings.Contains(moduleName, ":") {
return nil, fnerrors.InternalError("module names can't contain colons")
}
key := fmt.Sprintf("%s:%s", moduleName, secretFile)
l.mu.Lock()
defer l.mu.Unlock()
if existing, ok := l.cache[key]; ok {
return existing, nil
}
fsys := l.moduleFS(modules, moduleName)
if fsys == nil {
return nil, fnerrors.InternalError("%s: module is not loaded", moduleName)
}
loaded, err := loadSecretsFile(ctx, l.keyDir, moduleName, fsys, secretFile)
if err != nil {
return nil, err
}
l.cache[key] = loaded
return loaded, nil
}
func (l *localSecrets) moduleFS(modules pkggraph.ModuleResolver, moduleName string) fs.FS {
if l.workspaceModule == moduleName {
return l.workspaceFS
}
if modules != nil {
mod := modules.ResolveModule(moduleName)
if mod != nil {
return mod.ReadOnlyFS()
}
}
return nil
}
func loadSecretsFile(ctx context.Context, keyDir fs.FS, name string, fsys fs.FS, sourceFile string) (*Bundle, error) {
contents, err := fs.ReadFile(fsys, sourceFile)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, fnerrors.InternalError("%s: failed to read %q: %w", name, sourceFile, err)
}
return LoadBundle(ctx, keyDir, contents)
}
func lookupSecret(ctx context.Context, env *schema.Environment, secretRef *schema.PackageRef, lookup ...*Bundle) (*schema.SecretResult, error) {
key := &ValueKey{PackageName: secretRef.PackageName, Key: secretRef.Name, EnvironmentName: env.Name}
for _, src := range lookup {
value, err := src.Lookup(ctx, key)
if err != nil {
return nil, err
}
if value != nil {
return &schema.SecretResult{Value: value, FileContents: &schema.FileContents{Contents: value}}, nil
}
}
return nil, nil
}