Skip to content
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

Allow KeyStore to be configured with multiple keys #3335

Merged
merged 7 commits into from
May 16, 2024
Merged

Conversation

dmjb
Copy link
Member

@dmjb dmjb commented May 15, 2024

Fixes #3304

Add a new configuration structure for encryption keys. This allows a default key and algorithm to be specified, along with fallbacks. A backwards compatibility mechanism is provided which will allow the keystore to work with the existing configuration structure.

Summary

Provide a brief overview of the changes and the issue being addressed.
Explain the rationale and any background necessary for understanding the changes.
List dependencies required by this change, if any.

Fixes #(related issue)

Change Type

Mark the type of change your PR introduces:

  • Bug fix (resolves an issue without affecting existing features)
  • Feature (adds new functionality without breaking changes)
  • Breaking change (may impact existing functionalities or require documentation updates)
  • Documentation (updates or additions to documentation)
  • Refactoring or test improvements (no bug fixes or new functionality)

Testing

Outline how the changes were tested, including steps to reproduce and any relevant configurations.
Attach screenshots if helpful.

Review Checklist:

  • Reviewed my own code for quality and clarity.
  • Added comments to complex or tricky code sections.
  • Updated any affected documentation.
  • Included tests that validate the fix or feature.
  • Checked that related changes are merged.

Fixes #3304

Add a new configuration structure for encryption keys. This allows a
default key and algorithm to be specified, along with fallbacks. A
backwards compatibility mechanism is provided which will allow the
keystore to work with the existing configuration structure.
@dmjb dmjb requested a review from a team as a code owner May 15, 2024 13:56
Copy link
Member

@stacklokbot stacklokbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No Invisible Unicode Characters Detected.

@coveralls
Copy link

coveralls commented May 15, 2024

Coverage Status

coverage: 49.527% (+0.1%) from 49.407%
when pulling 392e072 on key-store-part-ii
into 1eb80d3 on main.

Copy link
Member

@stacklokbot stacklokbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No Invisible Unicode Characters Detected.

Copy link
Member

@stacklokbot stacklokbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No Mixed Scripts Detected.

@@ -297,3 +273,33 @@ func getDefaultValueForInt64(value string) (any, error) {
// Return the original error, not time.ParseDuration's error
return nil, err
}

func getDefaultValue(field reflect.StructField, value string, valueName string) any {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Factored out into a function to avoid cyclomatic complexity limits

defaultValue, err = strconv.ParseBool(value)
case reflect.Slice:
defaultValue = nil
case reflect.Map:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new addition

evankanderson
evankanderson previously approved these changes May 15, 2024
Copy link
Member

@evankanderson evankanderson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we ended up changing the configuration interface, but we didn't change the encryption / decryption interfaces, correct?

A few small suggestions, but this overall makes sense to me.

internal/config/server/crypto.go Outdated Show resolved Hide resolved
Comment on lines +41 to +42
KeyIDs []string `mapstructure:"key_ids"`
Algorithms []string `mapstructure:"algorithms"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a []struct{KeyID string, Algo string}? I'm wondering what happens if the two arrays are not the same length.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is expected that they will not always be the same length. For example, if we rotate a key, but do not change the algorithm, then we will add a fallback key but not a corresponding algorithm.

The selection of key/algorithm during decryption is based on the data we store in the database (see the EncryptedData struct in models.go) and the selection of the algorithm during encryption is based on the values in the DefaultCrypto struct. The fallback values state which algorithms and keys should be loaded at startup to support old data.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, okay. Works for me.

internal/config/server/crypto.go Outdated Show resolved Hide resolved
internal/crypto/keystores/keystore.go Outdated Show resolved Hide resolved
}

var keystoreCfg serverconfig.LocalFileKeyStoreConfig
err := mapstructure.Decode(config.KeyStore.Config, &keystoreCfg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because Config is a map[string]any, right?

(I'd like to minimize the total area of mapstructure in our code, though I'm willing to be convinced that I shouldn't)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's because the exact type of Config is not known until the Type field is read.

Perhaps this could be done as part of config parsing, but I felt it would be better to do at the point where we make use of the config.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said, I'm okay with this pattern -- it would be nice if we could 👍 or 👎 the config at parsing time, but that's already not the case.

Co-authored-by: Evan Anderson <evan@stacklok.com>
Copy link
Member

@stacklokbot stacklokbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No Invisible Unicode Characters Detected.

Copy link
Member

@stacklokbot stacklokbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No Mixed Scripts Detected.

Copy link
Member

@stacklokbot stacklokbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No Invisible Unicode Characters Detected.

Copy link
Member

@stacklokbot stacklokbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No Mixed Scripts Detected.

Copy link
Member

@stacklokbot stacklokbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No Invisible Unicode Characters Detected.

Copy link
Member

@stacklokbot stacklokbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No Mixed Scripts Detected.

Copy link
Member

@stacklokbot stacklokbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No Invisible Unicode Characters Detected.

Copy link
Member

@stacklokbot stacklokbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No Mixed Scripts Detected.

Copy link
Member

@stacklokbot stacklokbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No Invisible Unicode Characters Detected.

Copy link
Member

@stacklokbot stacklokbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No Mixed Scripts Detected.

@dmjb dmjb merged commit 686930a into main May 16, 2024
20 checks passed
@dmjb dmjb deleted the key-store-part-ii branch May 16, 2024 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hide encryption key fetching behind an interface
4 participants