Skip to content

Commit

Permalink
update auth and tls settings (#160)
Browse files Browse the repository at this point in the history
* update auth and tls settings

Co-authored-by: guangning <guangning@apache.org>
  • Loading branch information
nlu90 and tuteng authored May 21, 2021
1 parent 094eae7 commit 66e7b97
Show file tree
Hide file tree
Showing 26 changed files with 367 additions and 179 deletions.
13 changes: 12 additions & 1 deletion api/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,18 @@ type PulsarMessaging struct {
// webServiceURL
// brokerServiceURL
PulsarConfig string `json:"pulsarConfig,omitempty"`
AuthConfig string `json:"authConfig,omitempty"`

// The auth secret should contain the following fields
// clientAuthenticationPlugin
// clientAuthenticationParameters
AuthSecret string `json:"authSecret,omitempty"`

// The TLS secret should contain the following fields
// use_tls
// tls_allow_insecure
// hostname_verification_enabled
// tls_trust_cert_path
TLSSecret string `json:"tlsSecret,omitempty"`
}

type PodPolicy struct {
Expand Down
12 changes: 9 additions & 3 deletions config/crd/bases/compute.functionmesh.io_functionmeshes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2192,10 +2192,12 @@ spec:
type: string
pulsar:
properties:
authConfig:
authSecret:
type: string
pulsarConfig:
type: string
tlsSecret:
type: string
type: object
python:
properties:
Expand Down Expand Up @@ -4363,10 +4365,12 @@ spec:
type: string
pulsar:
properties:
authConfig:
authSecret:
type: string
pulsarConfig:
type: string
tlsSecret:
type: string
type: object
python:
properties:
Expand Down Expand Up @@ -6513,10 +6517,12 @@ spec:
type: string
pulsar:
properties:
authConfig:
authSecret:
type: string
pulsarConfig:
type: string
tlsSecret:
type: string
type: object
python:
properties:
Expand Down
4 changes: 3 additions & 1 deletion config/crd/bases/compute.functionmesh.io_functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2193,10 +2193,12 @@ spec:
type: string
pulsar:
properties:
authConfig:
authSecret:
type: string
pulsarConfig:
type: string
tlsSecret:
type: string
type: object
python:
properties:
Expand Down
4 changes: 3 additions & 1 deletion config/crd/bases/compute.functionmesh.io_sinks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2128,10 +2128,12 @@ spec:
type: string
pulsar:
properties:
authConfig:
authSecret:
type: string
pulsarConfig:
type: string
tlsSecret:
type: string
type: object
python:
properties:
Expand Down
4 changes: 3 additions & 1 deletion config/crd/bases/compute.functionmesh.io_sources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2103,10 +2103,12 @@ spec:
type: string
pulsar:
properties:
authConfig:
authSecret:
type: string
pulsarConfig:
type: string
tlsSecret:
type: string
type: object
python:
properties:
Expand Down
20 changes: 19 additions & 1 deletion config/samples/compute_v1alpha1_function.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ spec:
key: "password"
pulsar:
pulsarConfig: "test-pulsar"
#authConfig: "test-auth"
authSecret: "test-auth"
tlsSecret: "test-tls"
volumeMounts:
- mountPath: /cache
name: cache-volume
Expand Down Expand Up @@ -73,6 +74,23 @@ metadata:
data:
webServiceURL: http://test-pulsar-broker.default.svc.cluster.local:8080
brokerServiceURL: pulsar://test-pulsar-broker.default.svc.cluster.local:6650
---
apiVersion: v1
kind: Secret
metadata:
name: test-auth
stringData:
clientAuthenticationPlugin: admin
clientAuthenticationParameters: t0p-Secret
---
apiVersion: v1
kind: Secret
metadata:
name: test-tls
stringData:
tlsTrustCertsFilePath: "uvw"
tlsAllowInsecureConnection: "false"
tlsHostnameVerificationEnable: "true"
#---
#apiVersion: v1
#kind: ConfigMap
Expand Down
13 changes: 0 additions & 13 deletions config/samples/compute_v1alpha1_function_crypto.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ spec:
key: "password"
pulsar:
pulsarConfig: "test-pulsar"
#authConfig: "test-auth"
java:
jar: pulsar-functions-api-examples.jar
jarLocation: public/default/nlu-test-java-function
Expand All @@ -66,18 +65,6 @@ metadata:
data:
webServiceURL: http://test-pulsar-broker.default.svc.cluster.local:8080
brokerServiceURL: pulsar://test-pulsar-broker.default.svc.cluster.local:6650
#---
#apiVersion: v1
#kind: ConfigMap
#metadata:
# name: test-auth
#data:
# clientAuthenticationPlugin: "abc"
# clientAuthenticationParameters: "xyz"
# tlsTrustCertsFilePath: "uvw"
# useTls: "true"
# tlsAllowInsecureConnection: "false"
# tlsHostnameVerificationEnable: "true"
---
apiVersion: v1
data:
Expand Down
13 changes: 0 additions & 13 deletions config/samples/compute_v1alpha1_function_key_based_batcher.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ spec:
key: "password"
pulsar:
pulsarConfig: "test-pulsar"
#authConfig: "test-auth"
volumeMounts:
- mountPath: /cache
name: cache-volume
Expand Down Expand Up @@ -75,18 +74,6 @@ metadata:
data:
webServiceURL: http://test-pulsar-broker.default.svc.cluster.local:8080
brokerServiceURL: pulsar://test-pulsar-broker.default.svc.cluster.local:6650
#---
#apiVersion: v1
#kind: ConfigMap
#metadata:
# name: test-auth
#data:
# clientAuthenticationPlugin: "abc"
# clientAuthenticationParameters: "xyz"
# tlsTrustCertsFilePath: "uvw"
# useTls: "true"
# tlsAllowInsecureConnection: "false"
# tlsHostnameVerificationEnable: "true"
---
apiVersion: v1
data:
Expand Down
13 changes: 0 additions & 13 deletions config/samples/compute_v1alpha1_go_function.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ spec:
key: "password"
pulsar:
pulsarConfig: "test-go-pulsar"
#authConfig: "test-auth"
golang:
go: go_func_all
goLocation: public/default/nlu-test-go-function
Expand All @@ -48,18 +47,6 @@ metadata:
data:
webServiceURL: http://test-pulsar-broker.default.svc.cluster.local:8080
brokerServiceURL: pulsar://test-pulsar-broker.default.svc.cluster.local:6650
#---
#apiVersion: v1
#kind: ConfigMap
#metadata:
# name: test-auth
#data:
# clientAuthenticationPlugin: "abc"
# clientAuthenticationParameters: "xyz"
# tlsTrustCertsFilePath: "uvw"
# useTls: "true"
# tlsAllowInsecureConnection: "false"
# tlsHostnameVerificationEnable: "true"
---
apiVersion: v1
data:
Expand Down
13 changes: 0 additions & 13 deletions config/samples/compute_v1alpha1_py_function.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
key: "password"
pulsar:
pulsarConfig: "test-py-pulsar"
#authConfig: "test-auth"
python:
py: exclamation_function.py
pyLocation: public/default/nlu-test-py-function
Expand All @@ -49,18 +48,6 @@ metadata:
data:
webServiceURL: http://test-pulsar-broker.default.svc.cluster.local:8080
brokerServiceURL: pulsar://test-pulsar-broker.default.svc.cluster.local:6650
#---
#apiVersion: v1
#kind: ConfigMap
#metadata:
# name: test-auth
#data:
# clientAuthenticationPlugin: "abc"
# clientAuthenticationParameters: "xyz"
# tlsTrustCertsFilePath: "uvw"
# useTls: "true"
# tlsAllowInsecureConnection: "false"
# tlsHostnameVerificationEnable: "true"
---
apiVersion: v1
data:
Expand Down
59 changes: 43 additions & 16 deletions controllers/spec/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,11 @@ func MakePodTemplate(container *corev1.Container, volumes []corev1.Volume,
}
}

func MakeJavaFunctionCommand(downloadPath, packageFile, name, clusterName, details, memory, extraDependenciesDir string, authProvided bool) []string {
func MakeJavaFunctionCommand(downloadPath, packageFile, name, clusterName, details, memory, extraDependenciesDir string,
authProvided, tlsProvided bool) []string {
processCommand := setShardIDEnvironmentVariableCommand() + " && " +
strings.Join(getProcessJavaRuntimeArgs(name, packageFile, clusterName, details, memory, extraDependenciesDir, authProvided), " ")
strings.Join(getProcessJavaRuntimeArgs(name, packageFile, clusterName, details,
memory, extraDependenciesDir, authProvided, tlsProvided), " ")
if downloadPath != "" {
// prepend download command if the downPath is provided
downloadCommand := strings.Join(getDownloadCommand(downloadPath, packageFile), " ")
Expand All @@ -172,9 +174,11 @@ func MakeJavaFunctionCommand(downloadPath, packageFile, name, clusterName, detai
return []string{"sh", "-c", processCommand}
}

func MakePythonFunctionCommand(downloadPath, packageFile, name, clusterName, details string, authProvided bool) []string {
func MakePythonFunctionCommand(downloadPath, packageFile, name, clusterName, details string,
authProvided, tlsProvided bool) []string {
processCommand := setShardIDEnvironmentVariableCommand() + " && " +
strings.Join(getProcessPythonRuntimeArgs(name, packageFile, clusterName, details, authProvided), " ")
strings.Join(getProcessPythonRuntimeArgs(name, packageFile, clusterName,
details, authProvided, tlsProvided), " ")
if downloadPath != "" {
// prepend download command if the downPath is provided
downloadCommand := strings.Join(getDownloadCommand(downloadPath, packageFile), " ")
Expand Down Expand Up @@ -231,11 +235,10 @@ func hasPackageNamePrefix(packagesName string) bool {
}

func setShardIDEnvironmentVariableCommand() string {
tlsCommand := "if [ \"$useTls\" = \"true\" ]; then TLS_PARAMETERS=\"--use_tls $useTls --tls_allow_insecure $tlsAllowInsecureConnection --hostname_verification_enabled $tlsHostnameVerificationEnable --tls_trust_cert_path $tlsTrustCertsFilePath\"; else TLS_PARAMETERS=\"--use_tls false\"; fi"
return fmt.Sprintf("%s=${POD_NAME##*-} && echo shardId=${%s} && %s", EnvShardID, EnvShardID, tlsCommand)
return fmt.Sprintf("%s=${POD_NAME##*-} && echo shardId=${%s}", EnvShardID, EnvShardID)
}

func getProcessJavaRuntimeArgs(name, packageName, clusterName, details, memory, extraDependenciesDir string, authProvided bool) []string {
func getProcessJavaRuntimeArgs(name, packageName, clusterName, details, memory, extraDependenciesDir string, authProvided, tlsProvided bool) []string {
classPath := "/pulsar/instances/java-instance.jar"
if extraDependenciesDir != "" {
classPath = fmt.Sprintf("%s:%s/*", classPath, extraDependenciesDir)
Expand All @@ -254,12 +257,12 @@ func getProcessJavaRuntimeArgs(name, packageName, clusterName, details, memory,
"--jar",
packageName,
}
sharedArgs := getSharedArgs(details, clusterName, authProvided)
sharedArgs := getSharedArgs(details, clusterName, authProvided, tlsProvided)
args = append(args, sharedArgs...)
return args
}

func getProcessPythonRuntimeArgs(name, packageName, clusterName, details string, authProvided bool) []string {
func getProcessPythonRuntimeArgs(name, packageName, clusterName, details string, authProvided, tlsProvided bool) []string {
args := []string{
"exec",
"python",
Expand All @@ -274,13 +277,13 @@ func getProcessPythonRuntimeArgs(name, packageName, clusterName, details string,
"/pulsar/conf/functions-logging/console_logging_config.ini",
// TODO: Maybe we don't need installUserCodeDependencies, dependency_repository, and pythonExtraDependencyRepository
}
sharedArgs := getSharedArgs(details, clusterName, authProvided)
sharedArgs := getSharedArgs(details, clusterName, authProvided, tlsProvided)
args = append(args, sharedArgs...)
return args
}

// This method is suitable for Java and Python runtime, not include Go runtime.
func getSharedArgs(details, clusterName string, authProvided bool) []string {
func getSharedArgs(details, clusterName string, authProvided bool, tlsProvided bool) []string {
args := []string{
"--instance_id",
"${" + EnvShardID + "}",
Expand Down Expand Up @@ -312,7 +315,23 @@ func getSharedArgs(details, clusterName string, authProvided bool) []string {
"$clientAuthenticationParameters"}...)
}

args = append(args, []string{"$TLS_PARAMETERS"}...)
if tlsProvided {
args = append(args, []string{
"--use_tls",
"true",
"--tls_allow_insecure",
"$tlsAllowInsecureConnection",
"--hostname_verification_enabled",
"$tlsHostnameVerificationEnable",
"--tls_trust_cert_path",
"$tlsTrustCertsFilePath",
}...)
} else {
args = append(args, []string{
"--use_tls",
"false",
}...)
}

return args
}
Expand Down Expand Up @@ -418,17 +437,25 @@ func generateContainerEnv(secrets map[string]v1alpha1.SecretRef) []corev1.EnvVar
return vars
}

func generateContainerEnvFrom(messagingConfig string, authConfig string) []corev1.EnvFromSource {
func generateContainerEnvFrom(messagingConfig string, authSecret string, tlsSecret string) []corev1.EnvFromSource {
envs := []corev1.EnvFromSource{{
ConfigMapRef: &corev1.ConfigMapEnvSource{
LocalObjectReference: corev1.LocalObjectReference{Name: messagingConfig},
},
}}

if authConfig != "" {
if authSecret != "" {
envs = append(envs, corev1.EnvFromSource{
SecretRef: &corev1.SecretEnvSource{
LocalObjectReference: corev1.LocalObjectReference{Name: authSecret},
},
})
}

if tlsSecret != "" {
envs = append(envs, corev1.EnvFromSource{
ConfigMapRef: &corev1.ConfigMapEnvSource{
LocalObjectReference: corev1.LocalObjectReference{Name: authConfig},
SecretRef: &corev1.SecretEnvSource{
LocalObjectReference: corev1.LocalObjectReference{Name: tlsSecret},
},
})
}
Expand Down
11 changes: 5 additions & 6 deletions controllers/spec/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,11 @@ func TestMakeGoFunctionCommand(t *testing.T) {
innerCommands := strings.Split(commands[2], "&&")
assert.Equal(t, innerCommands[0], "SHARD_ID=${POD_NAME##*-} ")
assert.Equal(t, innerCommands[1], " echo shardId=${SHARD_ID} ")
assert.Equal(t, innerCommands[2], " if [ \"$useTls\" = \"true\" ]; then TLS_PARAMETERS=\"--use_tls $useTls --tls_allow_insecure $tlsAllowInsecureConnection --hostname_verification_enabled $tlsHostnameVerificationEnable --tls_trust_cert_path $tlsTrustCertsFilePath\"; else TLS_PARAMETERS=\"--use_tls false\"; fi ")
assert.True(t, strings.HasPrefix(innerCommands[3], " GO_FUNCTION_CONF"))
assert.Equal(t, innerCommands[4], " goFunctionConfigs=${GO_FUNCTION_CONF} ")
assert.Equal(t, innerCommands[5], " echo goFunctionConfigs=\"'${goFunctionConfigs}'\" ")
assert.Equal(t, innerCommands[6], " chmod +x /pulsar/go-func ")
assert.Equal(t, innerCommands[7], " exec /pulsar/go-func -instance-conf ${goFunctionConfigs}")
assert.True(t, strings.HasPrefix(innerCommands[2], " GO_FUNCTION_CONF"))
assert.Equal(t, innerCommands[3], " goFunctionConfigs=${GO_FUNCTION_CONF} ")
assert.Equal(t, innerCommands[4], " echo goFunctionConfigs=\"'${goFunctionConfigs}'\" ")
assert.Equal(t, innerCommands[5], " chmod +x /pulsar/go-func ")
assert.Equal(t, innerCommands[6], " exec /pulsar/go-func -instance-conf ${goFunctionConfigs}")
}

const TestClusterName string = "test-pulsar"
Expand Down
Loading

0 comments on commit 66e7b97

Please sign in to comment.