1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
// Copyright (c) Contributors to the Apptainer project, established as
// Apptainer a Series of LF Projects LLC.
// For website terms of use, trademark policy, privacy policy and other
// project policies see https://lfprojects.org/policies
// Copyright (c) 2019, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package cryptkey
import (
"fmt"
"testing"
"github.com/apptainer/apptainer/internal/pkg/test"
"github.com/pkg/errors"
)
const (
invalidPemPath = "nothing"
testPassphrase = "test"
badPemData = "bad data"
goodPemData = `-----BEGIN RSA PUBLIC KEY-----
MIICCgKCAgEAj29RUJcaXFzKKFhfzZpUTZLf5gc4G+hJbRgOxKiqxlbrTXS2sO73
W38KBs5+ZAj4JUfrxNbUYU9ZVFs4ikHhCIIblMUl5JCGYF00F0nDHIuaRPv5ywI/
Sf6A/+6JA2rDxzvp3C4g2ukPQCrCA+A8hCuM3Qbzv8TR4wGmt0k5SRqfIk2AWAaH
5Dk2bbWzkcLvwRN97/JO5XLrXxiYB1dU7a6tkA+4PChxieJK2y0DxWvPrBsijqj7
j2mogo5FKKqwZ91+2CtDhehDzrshcYdUkGDgjVYH4CNG1Wcw/o0jq3hyIIWteCXT
AmgrGbOfLy+zZq+QkxjxjLFRFm/6L26OMbtb2mjdpU6KbCJJvMhmBW7TwkbKYVIe
gEmb846oRchgG3H/uoR3tPyW6Q5I60S1+S3UQ9xTUNYeXgK9/PTH7w/hsSgqQRUP
HYVU9MBHplHFs+rpLqVjkB90cVaIJ7yVoErJJt/GgHJs0wypopOJ9y1xoK1G/FEv
m/lws02svkAKjIyQiCO3oyCXBa9C4EeATriKt0DvCBh2xM64drbMk5FqvEELKbno
gK3HyFm6tn6tqO0GsLuFYDPPJ8s96OqoTDOXvCNZUQW93ljLOvf8hKQjiueL7nCN
r3Oy11/EgEv3gdQeZ47PKgkevS5vqcT06KZKcIOsnz05ik9WPhZqTW8CAwEAAQ==
-----END RSA PUBLIC KEY-----`
)
func TestNewPlaintextKey(t *testing.T) {
test.DropPrivilege(t)
defer test.ResetPrivilege(t)
tests := []struct {
name string
keyInfo KeyInfo
expectedError error
}{
{
name: "unknown format",
keyInfo: KeyInfo{Format: Unknown},
expectedError: ErrUnsupportedKeyURI,
},
{
name: "passphrase",
keyInfo: KeyInfo{Format: Passphrase, Material: testPassphrase},
expectedError: nil,
},
{
name: "invalid pem",
keyInfo: KeyInfo{Format: PEM, Path: invalidPemPath},
expectedError: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := NewPlaintextKey(tt.keyInfo)
// We do not always use predefined errors so when dealing with errors, we compare the text associated
// to the error.
if (err != nil && tt.expectedError != nil && err.Error() != tt.expectedError.Error()) ||
((err == nil || tt.expectedError == nil) && err != tt.expectedError) {
t.Fatalf("test %s returned an unexpected error: %s vs. %s", tt.name, err, tt.expectedError)
}
})
}
}
func TestEncryptKey(t *testing.T) {
test.DropPrivilege(t)
defer test.ResetPrivilege(t)
tests := []struct {
name string
keyInfo KeyInfo
plaintext []byte
expectedError error
}{
{
name: "unknown format",
keyInfo: KeyInfo{Format: Unknown},
plaintext: []byte(""),
expectedError: ErrUnsupportedKeyURI,
},
{
name: "passphrase",
keyInfo: KeyInfo{Format: Passphrase, Material: testPassphrase},
plaintext: []byte(""),
expectedError: nil,
},
{
name: "invalid pem",
keyInfo: KeyInfo{Format: PEM, Path: invalidPemPath},
plaintext: []byte(""),
expectedError: errors.Wrap(fmt.Errorf("open nothing: no such file or directory"), "loading public key for key encryption: loading public key for key encryption"),
},
{
name: "invalid pem data",
keyInfo: KeyInfo{Format: ENV, Material: badPemData},
plaintext: []byte(""),
expectedError: fmt.Errorf("loading public key for key encryption: loading public key for key encryption: could not read bad data: no PEM data"),
},
{
name: "valid pem data",
keyInfo: KeyInfo{Format: ENV, Material: goodPemData},
plaintext: []byte(""),
expectedError: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := EncryptKey(tt.keyInfo, tt.plaintext)
// We do not always use predefined errors so when dealing with errors, we compare the text associated
// to the error.
if (err != nil && tt.expectedError != nil && err.Error() != tt.expectedError.Error()) ||
((err == nil || tt.expectedError == nil) && err != tt.expectedError) {
t.Fatalf("test %s returned an unexpected error: %s vs. %s", tt.name, err, tt.expectedError)
}
})
}
}
func TestPlaintextKey(t *testing.T) {
test.DropPrivilege(t)
defer test.ResetPrivilege(t)
// TestPlaintestKey reads a key from an image. Creating an image does not
// fit with unit tests testing so we only test error cases here.
const (
noimage = ""
)
tests := []struct {
name string
keyInfo KeyInfo
expectedError error
}{
{
name: "unknown format",
keyInfo: KeyInfo{Format: Unknown},
expectedError: ErrUnsupportedKeyURI,
},
{
name: "passphrase",
keyInfo: KeyInfo{Format: Passphrase, Material: testPassphrase},
expectedError: nil,
},
{
name: "invalid pem",
keyInfo: KeyInfo{Format: PEM, Path: invalidPemPath},
expectedError: fmt.Errorf("could not load PEM private key: loading public key for key encryption: open nothing: no such file or directory"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := PlaintextKey(tt.keyInfo, noimage)
// We do not always use predefined errors so when dealing with errors, we compare the text associated
// to the error.
if (err != nil && tt.expectedError != nil && err.Error() != tt.expectedError.Error()) ||
((err == nil || tt.expectedError == nil) && err != tt.expectedError) {
t.Fatalf("test %s returned an unexpected error: %s vs. %s", tt.name, err, tt.expectedError)
}
})
}
}
|