|
| 1 | +/* |
| 2 | +Copyright 2022 The Tekton Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package config |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + |
| 23 | + sc "github.com/tektoncd/pipeline/pkg/spire/config" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + // SpireConfigMapName is the name of the trusted resources configmap |
| 29 | + SpireConfigMapName = "config-spire" |
| 30 | + |
| 31 | + // SpireTrustDomain is the key to extract out the SPIRE trust domain to use |
| 32 | + SpireTrustDomain = "spire-trust-domain" |
| 33 | + // SpireSocketPath is the key to extract out the SPIRE agent socket for SPIFFE workload API |
| 34 | + SpireSocketPath = "spire-socket-path" |
| 35 | + // SpireServerAddr is the key to extract out the SPIRE server address for workload/node registration |
| 36 | + SpireServerAddr = "spire-server-addr" |
| 37 | + // SpireNodeAliasPrefix is the key to extract out the SPIRE node alias prefix to use |
| 38 | + SpireNodeAliasPrefix = "spire-node-alias-prefix" |
| 39 | + |
| 40 | + // SpireTrustDomainDefault is the default value for the SpireTrustDomain |
| 41 | + SpireTrustDomainDefault = "example.org" |
| 42 | + // SpireSocketPathDefault is the default value for the SpireSocketPath |
| 43 | + SpireSocketPathDefault = "unix:///spiffe-workload-api/spire-agent.sock" |
| 44 | + // SpireServerAddrDefault is the default value for the SpireServerAddr |
| 45 | + SpireServerAddrDefault = "spire-server.spire.svc.cluster.local:8081" |
| 46 | + // SpireNodeAliasPrefixDefault is the default value for the SpireNodeAliasPrefix |
| 47 | + SpireNodeAliasPrefixDefault = "/tekton-node/" |
| 48 | +) |
| 49 | + |
| 50 | +// NewSpireConfigFromMap creates a Config from the supplied map |
| 51 | +func NewSpireConfigFromMap(data map[string]string) (*sc.SpireConfig, error) { |
| 52 | + cfg := &sc.SpireConfig{} |
| 53 | + var ok bool |
| 54 | + if cfg.TrustDomain, ok = data[SpireTrustDomain]; !ok { |
| 55 | + cfg.TrustDomain = SpireTrustDomainDefault |
| 56 | + } |
| 57 | + if cfg.SocketPath, ok = data[SpireSocketPath]; !ok { |
| 58 | + cfg.SocketPath = SpireSocketPathDefault |
| 59 | + } |
| 60 | + if cfg.ServerAddr, ok = data[SpireServerAddr]; !ok { |
| 61 | + cfg.ServerAddr = SpireServerAddrDefault |
| 62 | + } |
| 63 | + if cfg.NodeAliasPrefix, ok = data[SpireNodeAliasPrefix]; !ok { |
| 64 | + cfg.NodeAliasPrefix = SpireNodeAliasPrefixDefault |
| 65 | + } |
| 66 | + if err := cfg.Validate(); err != nil { |
| 67 | + return nil, fmt.Errorf("failed to parse SPIRE configmap: %w", err) |
| 68 | + } |
| 69 | + return cfg, nil |
| 70 | +} |
| 71 | + |
| 72 | +// NewSpireConfigFromConfigMap creates a Config from the supplied ConfigMap |
| 73 | +func NewSpireConfigFromConfigMap(configMap *corev1.ConfigMap) (*sc.SpireConfig, error) { |
| 74 | + return NewSpireConfigFromMap(configMap.Data) |
| 75 | +} |
| 76 | + |
| 77 | +// GetSpireConfigName returns the name of Spire ConfigMap |
| 78 | +func GetSpireConfigName() string { |
| 79 | + if e := os.Getenv("CONFIG_SPIRE"); e != "" { |
| 80 | + return e |
| 81 | + } |
| 82 | + return SpireConfigMapName |
| 83 | +} |
0 commit comments