This repository has been archived by the owner on Jul 14, 2022. It is now read-only.
forked from rexray/rexray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_auth.go
77 lines (63 loc) · 2.01 KB
/
utils_auth.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
package utils
import (
"io/ioutil"
log "github.com/sirupsen/logrus"
gofig "github.com/akutz/gofig/types"
"github.com/akutz/gotil"
"github.com/rexray/rexray/libstorage/api/types"
)
// ParseAuthConfig returns a new AuthTokenConfig instance.
func ParseAuthConfig(
ctx types.Context,
config gofig.Config,
fields log.Fields,
roots ...string) (*types.AuthConfig, error) {
const prefix = types.ConfigServer + "."
if !isSetPrefix(config, prefix, types.ConfigServerAuthAllow, roots...) {
ctx.Debug("server auth config not defined")
return nil, nil
}
f := func(k string, v interface{}) {
if fields == nil {
return
}
fields[k] = v
ctx.WithField(k, v).Debug("parsed server auth property")
}
authConfig := &types.AuthConfig{Alg: "HS256"}
if isSetPrefix(config, prefix, types.ConfigServerAuthDisabled, roots...) {
authConfig.Disabled = getBool(
config, types.ConfigServerAuthDisabled, roots...)
f(types.ConfigServerAuthDisabled, authConfig.Disabled)
}
if isSetPrefix(config, prefix, types.ConfigServerAuthKey, roots...) {
szKey := getStringPrefix(
config, prefix, types.ConfigServerAuthKey, roots...)
f(types.ConfigServerAuthKey, szKey)
if gotil.FileExists(szKey) {
buf, err := ioutil.ReadFile(szKey)
if err != nil {
return nil, err
}
authConfig.Key = buf
} else {
authConfig.Key = []byte(szKey)
}
}
if isSetPrefix(config, prefix, types.ConfigServerAuthAlg, roots...) {
authConfig.Alg = getStringPrefix(
config, prefix, types.ConfigServerAuthAlg, roots...)
}
f(types.ConfigServerAuthAlg, authConfig.Alg)
if isSetPrefix(config, prefix, types.ConfigServerAuthAllow, roots...) {
authConfig.Allow = getStringSlicePrefix(
config, prefix, types.ConfigServerAuthAllow, roots...)
f(types.ConfigServerAuthAllow, authConfig.Allow)
}
if isSetPrefix(config, prefix, types.ConfigServerAuthDeny, roots...) {
authConfig.Deny = getStringSlicePrefix(
config, prefix, types.ConfigServerAuthDeny, roots...)
f(types.ConfigServerAuthDeny, authConfig.Deny)
}
return authConfig, nil
}