-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added creation of job template which is responsible for creating the …
…private and public key in provider/consumer mode.
- Loading branch information
Showing
10 changed files
with
289 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
deploy/ocs-operator/manifests/onboarding-secret-generator-binding.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
kind: ClusterRoleBinding | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
metadata: | ||
name: onboarding-secret-generator | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: onboarding-secret-generator | ||
subjects: | ||
- kind: ServiceAccount | ||
name: onboarding-secret-generator |
13 changes: 13 additions & 0 deletions
13
deploy/ocs-operator/manifests/onboarding-secret-generator-role.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
kind: ClusterRole | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
metadata: | ||
name: onboarding-secret-generator | ||
rules: | ||
- apiGroups: | ||
- "" | ||
resources: | ||
- secrets | ||
verbs: | ||
- get | ||
- list | ||
- create |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"os" | ||
|
||
openshiftv1 "github.com/openshift/api/template/v1" | ||
ocsv1 "github.com/red-hat-storage/ocs-operator/v4/api/v1" | ||
ocsv1alpha1 "github.com/red-hat-storage/ocs-operator/v4/api/v1alpha1" | ||
"github.com/red-hat-storage/ocs-operator/v4/controllers/util" | ||
corev1 "k8s.io/api/core/v1" | ||
v1 "k8s.io/api/core/v1" | ||
errruntime "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
apiruntime "k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
"k8s.io/klog" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
) | ||
|
||
const ( | ||
onboardingTicketPublicKeySecret = "onboarding-ticket-public-key" | ||
onboardingTicketPrivateKeySecret = "onboarding-ticket-private-key" | ||
) | ||
|
||
var ( | ||
ctx = context.Background() | ||
serverNamespace = os.Getenv(util.WatchNamespaceEnvVar) | ||
privatePem, publicPem string | ||
) | ||
|
||
func createSecret(cl client.Client, keyName, secretName string) { | ||
|
||
desiredSecret := GetProviderAPIServerSecret(keyName, secretName) | ||
actualSecret := &corev1.Secret{} | ||
|
||
err := cl.Get(ctx, client.ObjectKeyFromObject(desiredSecret), actualSecret) | ||
|
||
if err != nil && errruntime.IsNotFound(err) { | ||
err = cl.Create(ctx, desiredSecret) | ||
if err != nil { | ||
klog.Error(err, "Failed to create secret.") | ||
} | ||
} else if err != nil { | ||
klog.Error(err, "Failed to get secret.") | ||
|
||
} | ||
} | ||
|
||
func GetProviderAPIServerSecret(key string, secretName string) *corev1.Secret { | ||
|
||
return &corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: secretName, | ||
Namespace: serverNamespace, | ||
}, | ||
Immutable: func(flag bool) *bool { return &flag }(true), | ||
StringData: map[string]string{ | ||
"data": key, | ||
}, | ||
// Type: v1.SecretTypeServiceAccountToken, | ||
} | ||
} | ||
|
||
func newClient() (client.Client, error) { | ||
scheme := apiruntime.NewScheme() | ||
utilruntime.Must(ocsv1.AddToScheme(scheme)) | ||
utilruntime.Must(corev1.AddToScheme(scheme)) | ||
utilruntime.Must(ocsv1alpha1.AddToScheme(scheme)) | ||
utilruntime.Must(openshiftv1.AddToScheme(scheme)) | ||
|
||
config, err := config.GetConfig() | ||
if err != nil { | ||
klog.Error(err, "failed to get config check if KUBECOFIG is set.") | ||
return nil, err | ||
} | ||
client, err := client.New(config, client.Options{Scheme: scheme}) | ||
if err != nil { | ||
klog.Error(err, "failed to create controller-runtime client") | ||
return nil, err | ||
} | ||
|
||
return client, nil | ||
} | ||
|
||
func ConvertRsaPrivateKeyAsPemStr(privateKey *rsa.PrivateKey) string { | ||
privteKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey) | ||
privateKeyPem := pem.EncodeToMemory( | ||
&pem.Block{ | ||
Type: "RSA PRIVATE KEY", | ||
Bytes: privteKeyBytes, | ||
}, | ||
) | ||
return string(privateKeyPem) | ||
} | ||
|
||
func ConvertRsaPublicKeyAsPemStr(publicKey *rsa.PublicKey) (string, error) { | ||
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey) | ||
if err != nil { | ||
return "", err | ||
} | ||
publicKeyPem := pem.EncodeToMemory( | ||
&pem.Block{ | ||
Type: "RSA PUBLIC KEY", | ||
Bytes: publicKeyBytes, | ||
}, | ||
) | ||
|
||
return string(publicKeyPem), nil | ||
} | ||
|
||
func genratePrivateAndPublicKeys() { | ||
|
||
// Generate RSA key. | ||
var err error | ||
privateKey, err := rsa.GenerateKey(rand.Reader, 4096) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
publicKey := &privateKey.PublicKey | ||
|
||
// Export the keys to pem string | ||
//similar to key.pem & pubkey.pem | ||
privatePem = ConvertRsaPrivateKeyAsPemStr(privateKey) | ||
publicPem, _ = ConvertRsaPublicKeyAsPemStr(publicKey) | ||
|
||
} | ||
|
||
func main() { | ||
|
||
client, err := newClient() | ||
|
||
if err != nil { | ||
klog.Error(err, "failed to create controller-runtime client") | ||
return | ||
} | ||
|
||
// 1. Check public key secret exist or not | ||
secret := &v1.Secret{} | ||
err = client.Get(ctx, types.NamespacedName{Name: onboardingTicketPublicKeySecret, | ||
Namespace: serverNamespace}, secret) | ||
|
||
if err != nil { | ||
if errruntime.IsNotFound(err) { | ||
genratePrivateAndPublicKeys() | ||
createSecret(client, onboardingTicketPublicKeySecret, publicPem) | ||
createSecret(client, onboardingTicketPrivateKeySecret, privatePem) | ||
} else { | ||
klog.Error(err, "failed to get secret. ") | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
kind: ClusterRoleBinding | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
metadata: | ||
name: onboarding-secret-generator | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: onboarding-secret-generator | ||
subjects: | ||
- kind: ServiceAccount | ||
name: onboarding-secret-generator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
kind: ClusterRole | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
metadata: | ||
name: onboarding-secret-generator | ||
rules: | ||
- apiGroups: | ||
- "" | ||
resources: | ||
- secrets | ||
verbs: | ||
- get | ||
- list | ||
- create |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters