-
Notifications
You must be signed in to change notification settings - Fork 16
/
config.go
97 lines (80 loc) · 3.36 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
/*
Copyright 2019 NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ziti
import (
"crypto/x509"
"encoding/json"
"github.com/openziti/edge-api/rest_util"
"github.com/openziti/identity"
apis "github.com/openziti/sdk-golang/edge-apis"
"github.com/pkg/errors"
"os"
)
type Config struct {
//ZtAPI should be in the form of https://<domain>[:<port>]/edge/client/v1. For backwards compatability with single controller identities
ZtAPI string `json:"ztAPI"`
//ZtAPIs is an array of ZtAPI values, supersedes `ZtAPI`. ZtAPIs is used to make an initial connection to a controller.
ZtAPIs []string `json:"ztAPIs"`
//ConfigTypes is an array of string configuration types that will be requested from the controller
//for services.
ConfigTypes []string `json:"configTypes"`
//The ID field allows configurations is maintained for backwards compatability with previous SDK versions.
//If set, it will be used to set the Credentials field.
ID identity.Config `json:"id"`
//The Credentials field is used to authenticate with the Edge Client API. If the ID field is set, it will be used
//to populate this field with credentials.
Credentials apis.Credentials `json:"-"`
//EnableHa will signal to the SDK to query and use OIDC authentication which is required for HA controller setups.
//This is a temporary feature flag that will be removed and "default to true" at a later date.
EnableHa bool `json:"enableHa"`
}
// NewConfig will create a new Config object from a provided Ziti Edge Client API URL and identity configuration.
// The Ziti Edge Client API is usually in the format of `https://host:port/edge/client/v1`.
func NewConfig(ztApi string, idConfig identity.Config) *Config {
return &Config{
ZtAPI: ztApi,
ID: idConfig,
}
}
// NewConfigFromFile attempts to load a Config object from the provided path.
//
// The file that is indicated should be in the following format:
// ```
//
// {
// "ztAPI": "https://ziti.controller.example.com/edge/client/v1",
// "configTypes": ["config1", "config2"],
// "id": { "cert": "...", "key": "..." },
// }
//
// ```
func NewConfigFromFile(confFile string) (*Config, error) {
conf, err := os.ReadFile(confFile)
if err != nil {
return nil, errors.Errorf("config file (%s) is not found ", confFile)
}
c := Config{}
err = json.Unmarshal(conf, &c)
if err != nil {
return nil, errors.Errorf("failed to load ziti configuration (%s): %v", confFile, err)
}
return &c, nil
}
// GetControllerWellKnownCaPool will return a x509.CertPool. The target controller will not be verified via TLS and
// must be verified by some other means (i.e. enrollment JWT token).
//
// WARNING: This call is unauthenticated and should only be used for example purposes or expliciltly when an unauthenticated
// request is required.
func GetControllerWellKnownCaPool(controllerAddr string) (*x509.CertPool, error) {
return rest_util.GetControllerWellKnownCaPool(controllerAddr)
}