-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP - add first class secrets support
- Loading branch information
1 parent
4ae9efe
commit 3237e10
Showing
9 changed files
with
145 additions
and
0 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package secrets | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/go-logr/logr" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
|
||
hephv1 "github.com/dominodatalab/hephaestus/pkg/api/hephaestus/v1" | ||
) | ||
|
||
func ReadSecrets( | ||
ctx context.Context, | ||
obj *hephv1.ImageBuild, | ||
log logr.Logger, | ||
cfg *rest.Config, | ||
scheme *runtime.Scheme, | ||
) (map[string][]byte, error) { | ||
clientset, err := kubernetes.NewForConfig(cfg) | ||
if err != nil { | ||
return map[string][]byte{}, fmt.Errorf("failure to get kubernetes client: %w", err) | ||
} | ||
|
||
// TODO: add logging stuff | ||
// TODO: does it make more sense to project the secret into the filesystem somehow and then just use existing secrets? | ||
// Extracts secrets into data to pass to buildkit | ||
secretsData := make(map[string][]byte) | ||
for _, secretRef := range obj.Spec.Secrets { | ||
client := clientset.CoreV1().Secrets(secretRef.Namespace) | ||
|
||
secret, err := client.Get(ctx, secretRef.Name, metav1.GetOptions{}) | ||
|
||
if err != nil { | ||
return map[string][]byte{}, | ||
fmt.Errorf("failure to load secret '%s/%s': %w", secretRef.Namespace, secretRef.Name, err) | ||
} | ||
|
||
// prevent exfiltration of arbitrary secret values by using the presence of this lable | ||
if _, ok := secret.Labels[hephv1.AccessLabel]; !ok { | ||
return map[string][]byte{}, | ||
fmt.Errorf("secret '%s/%s' requires label %q", secretRef.Namespace, secretRef.Name, hephv1.AccessLabel) | ||
} | ||
|
||
// TODO: verify ImageBuild takes ownership of the secret and cleans it up after use to prevent cluster cruft | ||
// https://kubernetes.io/blog/2021/05/14/using-finalizers-to-control-deletion/ | ||
if _, ok := secret.Labels[hephv1.OwnedLabel]; ok { | ||
log.Info("taking ownership of secret '%s/%s'", "owner", obj.Name, | ||
"secret", fmt.Sprintf("%s/%s", secretRef.Namespace, secretRef.Name)) | ||
if err = controllerutil.SetOwnerReference(obj, secret, scheme); err != nil { | ||
// non-fatal error thats logged but ignored | ||
log.Error(err, "Ignoring error taking ownership of secret", | ||
"secret", fmt.Sprintf("%s/%s", secretRef.Namespace, secretRef.Name)) | ||
} | ||
} | ||
|
||
// builds a path for the secret like {namespace}/{name}/{key} to avoid hash key collisions | ||
for path, data := range secret.Data { | ||
name := strings.Join([]string{secret.Namespace, secret.Name, path}, "/") | ||
secretsData[name] = data | ||
log.Info("Read secret bytes", "path", name, "bytes", len(data)) | ||
} | ||
} | ||
|
||
return secretsData, nil | ||
} |