-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
157 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License.AGPL.txt in the project root for license information. | ||
|
||
package dbtest | ||
|
||
import ( | ||
"encoding/base64" | ||
db "github.com/gitpod-io/gitpod/components/gitpod-db/go" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func GetTestCipher(t *testing.T) (*db.AES256CBC, db.CipherMetadata) { | ||
t.Helper() | ||
|
||
// This is a test key also used in server tests - see components/gitpod-protocol/src/encryption/encryption-engine.spec.ts | ||
key, err := base64.StdEncoding.DecodeString("ZMaTPrF7s9gkLbY45zP59O0LTpLvDd/cgqPE9Ptghh8=") | ||
require.NoError(t, err) | ||
|
||
metadata := db.CipherMetadata{ | ||
Name: "default", | ||
Version: 1, | ||
} | ||
cipher, err := db.NewAES256CBCCipher(string(key), metadata) | ||
require.NoError(t, err) | ||
return cipher, metadata | ||
} |
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,66 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License.AGPL.txt in the project root for license information. | ||
|
||
package db | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"gorm.io/datatypes" | ||
) | ||
|
||
type EncryptedJSON[T any] datatypes.JSON | ||
|
||
func (j *EncryptedJSON[T]) EncryptedData() (EncryptedData, error) { | ||
var data EncryptedData | ||
err := json.Unmarshal(*j, &data) | ||
if err != nil { | ||
return EncryptedData{}, fmt.Errorf("failed to unmarshal encrypted json: %w", err) | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
func (j *EncryptedJSON[T]) Decrypt(decryptor Decryptor) (T, error) { | ||
var out T | ||
data, err := j.EncryptedData() | ||
if err != nil { | ||
return out, fmt.Errorf("failed to obtain encrypted data: %w", err) | ||
} | ||
|
||
b, err := decryptor.Decrypt(data) | ||
if err != nil { | ||
return out, fmt.Errorf("failed to decrypt encrypted json: %w", err) | ||
} | ||
|
||
err = json.Unmarshal(b, &out) | ||
if err != nil { | ||
return out, fmt.Errorf("failed to unmarshal encrypted json: %w", err) | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
func EncryptJSON[T any](encryptor Encryptor, data T) (EncryptedJSON[T], error) { | ||
b, err := json.Marshal(data) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal data into json: %w", err) | ||
} | ||
|
||
encrypted, err := encryptor.Encrypt(b) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to encrypt json: %w", err) | ||
} | ||
|
||
return NewEncryptedJSON[T](encrypted) | ||
} | ||
|
||
func NewEncryptedJSON[T any](data EncryptedData) (EncryptedJSON[T], error) { | ||
b, err := json.Marshal(data) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to serialize encrypted data into json: %w", err) | ||
} | ||
|
||
return b, nil | ||
} |
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,34 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License.AGPL.txt in the project root for license information. | ||
|
||
package db_test | ||
|
||
import ( | ||
db "github.com/gitpod-io/gitpod/components/gitpod-db/go" | ||
"github.com/gitpod-io/gitpod/components/gitpod-db/go/dbtest" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestEncryptJSON_DecryptJSON(t *testing.T) { | ||
cipher, _ := dbtest.GetTestCipher(t) | ||
|
||
type Data struct { | ||
First string | ||
Second int | ||
} | ||
|
||
data := Data{ | ||
First: "first", | ||
Second: 2, | ||
} | ||
|
||
encrypted, err := db.EncryptJSON(cipher, data) | ||
require.NoError(t, err) | ||
|
||
decrypted, err := encrypted.Decrypt(cipher) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, data, decrypted) | ||
} |
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
File renamed without changes.
File renamed without changes.