Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read broker token and CA from a mounted secret #300

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions pkg/resource/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ import (
"k8s.io/client-go/rest"
)

func GetAuthorizedRestConfig(apiServer, apiServerToken, caData string, tls *rest.TLSClientConfig,
func GetAuthorizedRestConfigFromData(apiServer, apiServerToken, caData string, tls *rest.TLSClientConfig,
gvr schema.GroupVersionResource, namespace string) (restConfig *rest.Config, authorized bool, err error) {
// First try a REST config without the CA trust chain
restConfig, err = BuildRestConfig(apiServer, apiServerToken, "", tls)
restConfig, err = BuildRestConfigFromData(apiServer, apiServerToken, "", tls)
if err != nil {
return
}

authorized, err = IsAuthorizedFor(restConfig, gvr, namespace)
if !authorized {
// Now try with the trust chain
restConfig, err = BuildRestConfig(apiServer, apiServerToken, caData, tls)
restConfig, err = BuildRestConfigFromData(apiServer, apiServerToken, caData, tls)
if err != nil {
return
}
Expand All @@ -54,7 +54,29 @@ func GetAuthorizedRestConfig(apiServer, apiServerToken, caData string, tls *rest
return
}

func BuildRestConfig(apiServer, apiServerToken, caData string, tls *rest.TLSClientConfig) (*rest.Config, error) {
func GetAuthorizedRestConfigFromFiles(apiServer, apiServerTokenFile, caFile string, tls *rest.TLSClientConfig,
dfarrell07 marked this conversation as resolved.
Show resolved Hide resolved
gvr schema.GroupVersionResource, namespace string) (restConfig *rest.Config, authorized bool, err error) {
// First try a REST config without the CA trust chain
restConfig, err = BuildRestConfigFromFiles(apiServer, apiServerTokenFile, "", tls)
if err != nil {
return
}

authorized, err = IsAuthorizedFor(restConfig, gvr, namespace)
if !authorized {
// Now try with the trust chain
restConfig, err = BuildRestConfigFromFiles(apiServer, apiServerTokenFile, caFile, tls)
if err != nil {
return
}

authorized, err = IsAuthorizedFor(restConfig, gvr, namespace)
}

return
}

func BuildRestConfigFromData(apiServer, apiServerToken, caData string, tls *rest.TLSClientConfig) (*rest.Config, error) {
if tls == nil {
tls = &rest.TLSClientConfig{}
}
Expand All @@ -75,6 +97,22 @@ func BuildRestConfig(apiServer, apiServerToken, caData string, tls *rest.TLSClie
}, nil
}

func BuildRestConfigFromFiles(apiServer, apiServerTokenFile, caFile string, tls *rest.TLSClientConfig) (*rest.Config, error) {
if tls == nil {
tls = &rest.TLSClientConfig{}
}

if !tls.Insecure && caFile != "" {
tls.CAFile = caFile
}

return &rest.Config{
Host: fmt.Sprintf("https://%s", apiServer),
TLSClientConfig: *tls,
BearerTokenFile: apiServerTokenFile,
}, nil
}

func IsAuthorizedFor(restConfig *rest.Config, gvr schema.GroupVersionResource, namespace string) (bool, error) {
client, err := dynamic.NewForConfig(restConfig)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions pkg/syncer/broker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type brokerSpecification struct {
RemoteNamespace string
Insecure bool `default:"false"`
Ca string
Secret string
}

const brokerConfigPrefix = "broker_k8s"
Expand Down Expand Up @@ -61,3 +62,7 @@ func EnvironmentVariable(setting string) string {

panic(fmt.Sprintf("unknown Broker setting %s", setting))
}

func SecretPath(secretName string) string {
return fmt.Sprintf("/run/secrets/submariner.io/%s", secretName)
}
11 changes: 9 additions & 2 deletions pkg/syncer/broker/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package broker

import (
"fmt"
"path/filepath"
"reflect"
"time"

Expand Down Expand Up @@ -268,8 +269,14 @@ func createBrokerClient(config *SyncerConfig) error {

config.BrokerNamespace = spec.RemoteNamespace

config.BrokerRestConfig, authorized, err = resource.GetAuthorizedRestConfig(spec.APIServer, spec.APIServerToken, spec.Ca,
&rest.TLSClientConfig{Insecure: spec.Insecure}, *gvr, spec.RemoteNamespace)
if spec.Secret != "" {
config.BrokerRestConfig, authorized, err = resource.GetAuthorizedRestConfigFromFiles(spec.APIServer,
filepath.Join(SecretPath(spec.Secret), "token"), filepath.Join(SecretPath(spec.Secret), "ca.crt"),
&rest.TLSClientConfig{Insecure: spec.Insecure}, *gvr, spec.RemoteNamespace)
} else {
config.BrokerRestConfig, authorized, err = resource.GetAuthorizedRestConfigFromData(spec.APIServer, spec.APIServerToken, spec.Ca,
&rest.TLSClientConfig{Insecure: spec.Insecure}, *gvr, spec.RemoteNamespace)
}
}

if !authorized {
Expand Down