forked from ajmyyra/ambassador-auth-oidc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
162 lines (147 loc) · 4.72 KB
/
config.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
package main
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"log"
mathrand "math/rand"
"os"
"strings"
"github.com/go-redis/redis"
)
// Config global configuration
type Config struct {
ListenPort int `json:"listen_port"`
BasicAuthPrefixes string `json:"basic_auth_prefixes"`
SigningKeyFile string `json:"signing_key_file"`
TracingURL string `json:"tracing_url"`
TracingService string `json:"tracing_service"`
Tenants map[string]*TenantConfig `json:"tenants"`
Redis *redis.Options `json:"redis"`
}
// TenantConfig per tenant configuration
type TenantConfig struct {
OIDCProvider string `json:"oidc_provider"`
OAuth2Provider *OAuth2ProviderConfig `json:"oauth2_provider"`
RedirectPath string `json:"redirect_path"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
OIDCScopes string `json:"oidc_scopes"`
BrokenAuthHeaderProvider bool `json:"broken_auth_header_provider"`
SkipClientIDCheck bool `json:"skip_client_id_check"`
UserPath string `json:"user_path"`
GroupsPath string `json:"groups_path"`
}
// OAuth2ProviderConfig oauth2 (non OIDC) provider configuration
type OAuth2ProviderConfig struct {
Issuer string `json:"issuer"`
AuthURL string `json:"authorization_endpoint"`
TokenURL string `json:"token_endpoint"`
JWKSURL string `json:"jwks_uri"`
}
const defaultRedirectPath = "/login/oidc"
const defaultUserPath = "preferred_username"
const defaultGroupsPath = "resource_access.{client_id}.roles"
var config Config
var redirectPaths []string
var oidcConfigMap = make(map[string]*oidcConfig)
var redisdb *redis.Client
var signingKey *rsa.PrivateKey
var nonceBytes = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
func init() {
// get configuration file name
filename := os.Getenv("CONFIG_FILE")
if len(filename) == 0 {
filename = "./config.json"
}
// parse configuration file
configFile, err := os.Open(filename)
defer configFile.Close()
if err != nil {
log.Fatal("Error opening config file ", filename, " ", err.Error())
}
jsonParser := json.NewDecoder(configFile)
err = jsonParser.Decode(&config)
if err != nil {
log.Fatal("Error decoding config file ", filename, " ", err.Error())
}
// set defaults and prepare per tenant OIDC configuration for lazy initialization
if config.ListenPort == 0 {
config.ListenPort = 8080
}
redirectPaths = make([]string, 0)
for tenant, tenantConfig := range config.Tenants {
if tenantConfig.RedirectPath == "" {
tenantConfig.RedirectPath = defaultRedirectPath
}
var duplicate = false
for i := range redirectPaths {
if redirectPaths[i] == tenantConfig.RedirectPath {
duplicate = true
}
}
if !duplicate {
redirectPaths = append(redirectPaths, tenantConfig.RedirectPath)
}
if tenantConfig.UserPath == "" {
tenantConfig.UserPath = defaultUserPath
}
if tenantConfig.GroupsPath == "" {
tenantConfig.GroupsPath = strings.Replace(defaultGroupsPath, "{client_id}", tenantConfig.ClientID, -1)
}
oidcConfigMap[tenant] = &oidcConfig{tenantConfig: tenantConfig}
}
// initialize Redis client
if config.Redis != nil {
redisdb = redis.NewClient(config.Redis)
_, err := redisdb.Ping().Result()
if err != nil {
log.Fatal("Problem connecting to Redis: ", err.Error())
}
}
// get signing key if must sign
if config.SigningKeyFile != "" {
bytes, err := ioutil.ReadFile(config.SigningKeyFile)
if err == nil {
signingKey, err = parsePrivateKey(bytes)
}
if err != nil {
log.Fatal("Error retrieving signing key from file ", config.SigningKeyFile, " ", err.Error())
}
}
}
func parsePrivateKey(pemBytes []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(pemBytes)
if block == nil {
return nil, errors.New("ssh: no key found")
}
switch block.Type {
case "RSA PRIVATE KEY":
rsa, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return rsa, nil
default:
return nil, fmt.Errorf("ssh: unsupported key type %q", block.Type)
}
}
func signedNonce() string {
nonce := make([]byte, 32)
for i := range nonce {
nonce[i] = nonceBytes[mathrand.Intn(len(nonceBytes))]
}
snonce := string(nonce)
if signingKey != nil {
signed, _ := signingKey.Sign(rand.Reader, nonce, crypto.Hash(0))
snonce = snonce + "." + base64.StdEncoding.EncodeToString(signed)
}
return snonce
}