-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use AES-256-GCM encryption by default
Fixes #3317 Use GCM encryption as the default encryption algorithm for Minder. At this point, we now have a hard requirement to use the new EncryptedData structure in the database - the old columns (for the access token, and for the redirect URL) do not track the algorithm used. As part of the migration away from the old columns, I have made the following changes in this PR: 1. Make the old columns nullable 2. Stop writing to the old columns 3. Change all unit tests which relied on the old columns
- Loading branch information
Showing
25 changed files
with
291 additions
and
79 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
19 changes: 19 additions & 0 deletions
19
database/migrations/000059_encrypted_token_nullable.down.sql
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,19 @@ | ||
-- Copyright 2024 Stacklok, Inc | ||
-- | ||
-- Licensed under the Apache License, Version 2.0 (the "License"); | ||
-- you may not use this file except in compliance with the License. | ||
-- You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
|
||
BEGIN; | ||
|
||
ALTER TABLE provider_access_tokens ALTER COLUMN encrypted_token SET NOT NULL; | ||
|
||
COMMIT; |
19 changes: 19 additions & 0 deletions
19
database/migrations/000059_encrypted_token_nullable.up.sql
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,19 @@ | ||
-- Copyright 2024 Stacklok, Inc | ||
-- | ||
-- Licensed under the Apache License, Version 2.0 (the "License"); | ||
-- you may not use this file except in compliance with the License. | ||
-- You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
|
||
BEGIN; | ||
|
||
ALTER TABLE provider_access_tokens ALTER COLUMN encrypted_token DROP NOT NULL; | ||
|
||
COMMIT; |
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,101 @@ | ||
// Copyright 2024 Stacklok, Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package algorithms_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/stacklok/minder/internal/crypto/algorithms" | ||
) | ||
|
||
func TestCFBEncrypt(t *testing.T) { | ||
t.Parallel() | ||
|
||
scenarios := []struct { | ||
Name string | ||
Key []byte | ||
Plaintext []byte | ||
ExpectedError string | ||
}{ | ||
{ | ||
Name: "CFB Encrypt rejects oversized plaintext", | ||
Key: key, | ||
Plaintext: make([]byte, 33*1024*1024), // 33MiB | ||
ExpectedError: algorithms.ErrExceedsMaxSize.Error(), | ||
}, | ||
{ | ||
Name: "CFB encrypts plaintext", | ||
Key: key, | ||
Plaintext: []byte(plaintext), | ||
}, | ||
} | ||
|
||
for _, scenario := range scenarios { | ||
t.Run(scenario.Name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
result, err := cfb.Encrypt(scenario.Plaintext, scenario.Key) | ||
if scenario.ExpectedError == "" { | ||
require.NoError(t, err) | ||
// validate by decrypting | ||
decrypted, err := cfb.Decrypt(result, key) | ||
require.NoError(t, err) | ||
require.Equal(t, scenario.Plaintext, decrypted) | ||
} else { | ||
require.ErrorContains(t, err, scenario.ExpectedError) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// This doesn't test decryption - that is tested in the happy path of the encrypt test | ||
func TestCFBDecrypt(t *testing.T) { | ||
t.Parallel() | ||
|
||
scenarios := []struct { | ||
Name string | ||
Key []byte | ||
Ciphertext []byte | ||
ExpectedError string | ||
}{ | ||
{ | ||
Name: "CFB Decrypt rejects short key", | ||
Key: []byte{0xFF}, | ||
Ciphertext: []byte(plaintext), | ||
ExpectedError: "ciphertext too short to decrypt", | ||
}, | ||
{ | ||
Name: "CFB Decrypt rejects undersized ciphertext", | ||
Key: key, | ||
Ciphertext: []byte{0xFF}, | ||
ExpectedError: "ciphertext too short to decrypt", | ||
}, | ||
} | ||
|
||
for _, scenario := range scenarios { | ||
t.Run(scenario.Name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
_, err := cfb.Decrypt(scenario.Ciphertext, scenario.Key) | ||
require.ErrorContains(t, err, scenario.ExpectedError) | ||
}) | ||
} | ||
} | ||
|
||
var ( | ||
cfb = algorithms.AES256CFBAlgorithm{} | ||
) |
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
Oops, something went wrong.