-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvault.go
109 lines (92 loc) · 3.02 KB
/
vault.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
package repository
import (
"fmt"
"log/slog"
"github.com/hashicorp/vault/api"
"github.com/reugn/auth-server/internal/util/env"
)
// Environment variables to configure VaultRepository.
const (
envVaultAddr = "AUTH_SERVER_VAULT_ADDR"
envVaultToken = "AUTH_SERVER_VAULT_TOKEN"
envVaultBasicKey = "AUTH_SERVER_VAULT_BASIC_KEY"
envVaultAuthKey = "AUTH_SERVER_VAULT_AUTHORIZATION_KEY"
)
// vaultConfig contains VaultRepository configuration properties.
type vaultConfig struct {
vaultAddr string
vaultToken string
basicAuthKeyPrefix string
authorizationKeyPrefix string
}
// VaultRepository implements the Repository interface using HashiCorp Vault
// as the storage backend.
type VaultRepository struct {
client *api.Client
config vaultConfig
}
var _ Repository = (*VaultRepository)(nil)
func getVaultConfig() vaultConfig {
// set defaults
config := vaultConfig{
vaultAddr: "localhost:8200",
basicAuthKeyPrefix: "secret/basic",
authorizationKeyPrefix: "secret/authorization",
}
// read configuration from environment variables
env.ReadString(&config.vaultAddr, envVaultAddr)
env.ReadString(&config.vaultToken, envVaultToken)
env.ReadString(&config.basicAuthKeyPrefix, envVaultBasicKey)
env.ReadString(&config.authorizationKeyPrefix, envVaultAuthKey)
return config
}
// NewVault returns a new VaultRepository using environment variables for configuration.
func NewVault() (*VaultRepository, error) {
config := getVaultConfig() // read configuration
apiConfig := &api.Config{
Address: config.vaultAddr,
}
client, err := api.NewClient(apiConfig)
if err != nil {
return nil, err
}
client.SetToken(config.vaultToken)
return &VaultRepository{
client: client,
config: config,
}, nil
}
// AuthenticateBasic validates the basic username and password before issuing a JWT.
// It uses the bcrypt password-hashing function to validate the password.
func (vr *VaultRepository) AuthenticateBasic(username string, password string) *UserDetails {
path := fmt.Sprintf("%s/%s", vr.config.basicAuthKeyPrefix, username)
secret, err := vr.client.Logical().Read(path)
if err != nil {
slog.Error("Failed to read path", "path", path, "err", err)
return nil
}
hashed, ok := secret.Data["password"].(string)
if !ok || !pwdMatch(hashed, password) {
slog.Debug("Failed to authenticate", "user", username)
return nil
}
return &UserDetails{
UserName: username,
UserRole: secret.Data["role"].(UserRole),
}
}
// AuthorizeRequest checks if the role has permissions to access the endpoint.
func (vr *VaultRepository) AuthorizeRequest(userRole UserRole, request RequestDetails) bool {
path := fmt.Sprintf("%s/%s", vr.config.authorizationKeyPrefix, userRole)
secret, err := vr.client.Logical().Read(path)
if err != nil {
slog.Error("Failed to read path", "path", path, "err", err)
return false
}
scopes, ok := secret.Data["scopes"].([]map[string]string)
if !ok {
slog.Error("Error reading scopes", "role", userRole)
return false
}
return isAuthorizedRequest(scopes, request)
}