-
Notifications
You must be signed in to change notification settings - Fork 2
Add ability to generate deploy keys to gitRepo controller #354
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
Changes from 2 commits
7be480a
c1d5d72
184f734
7d67a91
56b149c
4588bb3
14524d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,14 +4,17 @@ import ( | |
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/charmbracelet/keygen" | ||
| "github.com/go-logr/logr" | ||
| synv1alpha1 "github.com/projectsyn/lieutenant-operator/api/v1alpha1" | ||
| "go.uber.org/multierr" | ||
| corev1 "k8s.io/api/core/v1" | ||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "k8s.io/apimachinery/pkg/util/json" | ||
| "k8s.io/apimachinery/pkg/util/sets" | ||
| "k8s.io/utils/ptr" | ||
|
|
@@ -25,6 +28,11 @@ import ( | |
| "github.com/projectsyn/lieutenant-operator/pipeline" | ||
| ) | ||
|
|
||
| const DEPLOY_KEY_NAME_INFIX = "-deploy-key-" | ||
| const DEPLOY_KEY_SECRET_PUBKEY = "publicKey" | ||
| const DEPLOY_KEY_SECRET_PRIVKEY = "privateKey" | ||
| const DEPLOY_KEY_SECRET_TYPE = "type" | ||
|
|
||
| func Steps(obj pipeline.Object, data *pipeline.Context) pipeline.Result { | ||
| return steps(obj, data, manager.GetGitClient) | ||
| } | ||
|
|
@@ -101,6 +109,10 @@ func steps(obj pipeline.Object, data *pipeline.Context, getGitClient gitClientFa | |
| return pipeline.Result{Err: handleRepoError(data.Context, fmt.Errorf("ensure ci variables: %w", err), instance, data.Client)} | ||
| } | ||
|
|
||
| if err := ensureGeneratedDeployKeys(data.Context, data.Client, instance); err != nil { | ||
| return pipeline.Result{Err: handleRepoError(data.Context, fmt.Errorf("ensure ci variables: %w", err), instance, data.Client)} | ||
| } | ||
|
|
||
| err = repo.CommitTemplateFiles() | ||
| if err != nil { | ||
| return pipeline.Result{Err: handleRepoError(data.Context, err, instance, data.Client)} | ||
|
|
@@ -252,6 +264,74 @@ func ensureCIVariables(ctx context.Context, cli client.Client, instance *synv1al | |
| return nil | ||
| } | ||
|
|
||
| // ensureGeneratedDeployKeys ensures that the repo's `generateDeployKey` entries | ||
| // all have a corresponding `deployKey` entry, generating SSH keys as required | ||
| // and storing them in individual secrets. | ||
| func ensureGeneratedDeployKeys(ctx context.Context, cli client.Client, instance *synv1alpha1.GitRepo) error { | ||
| for genKey, settings := range instance.Spec.GeneratedDeployKeys { | ||
| secretName := instance.Name + DEPLOY_KEY_NAME_INFIX + genKey | ||
| secretNSName := types.NamespacedName{Name: secretName, Namespace: instance.Namespace} | ||
| secret := &corev1.Secret{} | ||
|
|
||
| err := cli.Get(ctx, secretNSName, secret) | ||
| if err != nil { | ||
| if !apierrors.IsNotFound(err) { | ||
| return err | ||
| } | ||
| err = generateNewDeployKeySecret(ctx, cli, settings, secretNSName, secret) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| pubkeyB, ok := secret.Data[DEPLOY_KEY_SECRET_PUBKEY] | ||
| if !ok { | ||
| return fmt.Errorf("could not retrieve deploy key from secret: missing key: %s", DEPLOY_KEY_SECRET_PUBKEY) | ||
| } | ||
| pubkey := string(pubkeyB[:]) | ||
| parts := strings.Split(pubkey, " ") | ||
|
|
||
| oldKey, ok := instance.Spec.DeployKeys[genKey] | ||
| if !ok || oldKey.Key != parts[1] { | ||
| if instance.Spec.DeployKeys == nil { | ||
| instance.Spec.DeployKeys = make(map[string]synv1alpha1.DeployKey) | ||
| } | ||
| instance.Spec.DeployKeys[genKey] = synv1alpha1.DeployKey{ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer tracking the generated deploy keys separately from externally provisioned deploy keys (I'd probably put the generated keys in Afaict, with the current approach, users could create chaos by requesting a generated deploy key named
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| Type: parts[0], | ||
| Key: parts[1], | ||
| WriteAccess: settings.WriteAccess, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func generateNewDeployKeySecret(ctx context.Context, cli client.Client, settings synv1alpha1.DeployKeyTemplate, secretName types.NamespacedName, secretRef *corev1.Secret) error { | ||
|
HappyTetrahedron marked this conversation as resolved.
Outdated
|
||
| keyType := keygen.Ed25519 | ||
| if settings.Type == "ssh-rsa" { | ||
| keyType = keygen.RSA | ||
| } | ||
|
|
||
| kp, err := keygen.New( | ||
| "/tmp/"+secretName.Name, | ||
| keygen.WithKeyType(keyType), | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| secretRef.Name = secretName.Name | ||
| secretRef.Namespace = secretName.Namespace | ||
|
|
||
| secretRef.Data = make(map[string][]byte) | ||
|
|
||
| secretRef.Data[DEPLOY_KEY_SECRET_PUBKEY] = kp.RawAuthorizedKey() | ||
| secretRef.Data[DEPLOY_KEY_SECRET_PRIVKEY] = kp.RawPrivateKey() | ||
|
|
||
| return cli.Create(ctx, secretRef) | ||
| } | ||
|
|
||
| // valueFromEnvVar returns the value of an envVar. It returns an error if the envVar is invalid or the value cannot be retrieved. | ||
| // EnvVars with both value and valueFrom are invalid. | ||
| // An envVar with no value and no valueFrom returns an empty string. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.