-
Notifications
You must be signed in to change notification settings - Fork 71
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
Refactor Encryption logic for improved testing #645
Merged
mallardduck
merged 8 commits into
rancher:main
from
mallardduck:refactor-encryption-config
Jan 14, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8c96dac
move encryption config specific functions to new package
mallardduck 9913032
refactor to use new package for encryption config
mallardduck fa6d7ab
move testdata to correct path
mallardduck 047bd07
Expand encryption tests to cover error assertions too
mallardduck fc36a9b
Additional refactor for testability
mallardduck 194dfc1
update controllers to use new transformer logic
mallardduck 5781fca
Fix context key
mallardduck 9158c73
fix goimports
mallardduck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,78 @@ | ||
package encryptionconfig | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/rancher/backup-restore-operator/pkg/util" | ||
v1 "github.com/rancher/wrangler/v3/pkg/generated/controllers/core/v1" | ||
|
||
"github.com/sirupsen/logrus" | ||
v1core "k8s.io/api/core/v1" | ||
v2 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
k8sEncryptionconfig "k8s.io/apiserver/pkg/server/options/encryptionconfig" | ||
storagevalue "k8s.io/apiserver/pkg/storage/value" | ||
"k8s.io/apiserver/pkg/storage/value/encrypt/identity" | ||
) | ||
|
||
type contextKey string | ||
|
||
var tempConfigPathKey = contextKey("tmpConfigPath") | ||
|
||
const EncryptionProviderConfigKey = "encryption-provider-config.yaml" | ||
|
||
func GetEncryptionConfigSecret(secrets v1.SecretController, encryptionConfigSecretName string) (*v1core.Secret, error) { | ||
// EncryptionConfig secret ns is hardcoded to ns of controller in chart's ns | ||
// kubectl create secret generic test-encryptionconfig --from-file=./encryption-provider-config.yaml | ||
logrus.Infof("Get encryption config from namespace %v", util.GetChartNamespace()) | ||
encryptionConfigSecret, err := secrets.Get(util.GetChartNamespace(), encryptionConfigSecretName, v2.GetOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return encryptionConfigSecret, nil | ||
} | ||
|
||
func GetEncryptionTransformersFromSecret(ctx context.Context, encryptionConfigSecret *v1core.Secret) (k8sEncryptionconfig.StaticTransformers, error) { | ||
fileHandle, err := PrepareEncryptionConfigSecretTempConfig(encryptionConfigSecret) | ||
// we defer file removal till here to ensure it's around for all of PrepareEncryptionTransformersFromConfig | ||
defer os.Remove(EncryptionProviderConfigKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ctx = context.WithValue(ctx, tempConfigPathKey, fileHandle.Name()) | ||
return PrepareEncryptionTransformersFromConfig(ctx, EncryptionProviderConfigKey) | ||
} | ||
|
||
func PrepareEncryptionConfigSecretTempConfig(encryptionConfigSecret *v1core.Secret) (*os.File, error) { | ||
encryptionConfigBytes, ok := encryptionConfigSecret.Data[EncryptionProviderConfigKey] | ||
if !ok { | ||
return nil, fmt.Errorf("no encryptionConfig provided") | ||
} | ||
err := os.WriteFile(EncryptionProviderConfigKey, encryptionConfigBytes, os.ModePerm) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Open the file for reading (or other operations) and return the handle | ||
file, err := os.Open(EncryptionProviderConfigKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return file, nil | ||
} | ||
|
||
func PrepareEncryptionTransformersFromConfig(ctx context.Context, encryptionProviderPath string) (k8sEncryptionconfig.StaticTransformers, error) { | ||
apiServerID := "" | ||
encryptionConfig, err := k8sEncryptionconfig.LoadEncryptionConfig(ctx, encryptionProviderPath, false, apiServerID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return encryptionConfig.Transformers, nil | ||
} | ||
|
||
func IsDefaultEncryptionTransformer(transformer storagevalue.Transformer) bool { | ||
return transformer == identity.NewEncryptCheckTransformer() | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't notice this before, but you we should be careful about opening files inside a function and expecting others to close said file.
Can lead to concurrent closes or file descriptor leaks.
We should look into alternative return types instead of
os.File
. I also don't see this being used by anything other than the tests at the moment.Will we need this inside the controller logic?