Skip to content

Commit

Permalink
refactor: gettin fumpty wid it
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-primrose committed Oct 3, 2024
1 parent 870e716 commit e91ff3e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 63 deletions.
125 changes: 64 additions & 61 deletions pkg/security/decrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,75 +8,78 @@ import (
"syscall"
"testing"

"github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/config"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"

"github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/config"
)

var validKey = "ThisismyveryStrongkey32byteslong"
var wrongKey = "ThisismyveryStrongkey32bytelong!"
var shortKey = "shortKey"
var missingKey = ""
var validMessageText = "Hello, World!"
var invalidMessageText = "Invalid_Base64_String!@#"
var expectedConfigFile = config.Configuration{
ID: 1234,
Name: "Test",
Configuration: config.RemoteManagement{
GeneralSettings: config.GeneralSettings{
SharedFQDN: true,
NetworkInterfaceEnabled: 1,
PingResponseEnabled: true,
},
Network: config.Network{
Wired: config.Wired{
DHCPEnabled: true,
IPSyncEnabled: true,
SharedStaticIP: true,
IPAddress: "",
SubnetMask: "",
DefaultGateway: "",
PrimaryDNS: "",
SecondaryDNS: "",
Authentication: "",
var (
validKey = "ThisismyveryStrongkey32byteslong"
wrongKey = "ThisismyveryStrongkey32bytelong!"
shortKey = "shortKey"
missingKey = ""
validMessageText = "Hello, World!"
invalidMessageText = "Invalid_Base64_String!@#"
expectedConfigFile = config.Configuration{
ID: 1234,
Name: "Test",
Configuration: config.RemoteManagement{
GeneralSettings: config.GeneralSettings{
SharedFQDN: true,
NetworkInterfaceEnabled: 1,
PingResponseEnabled: true,
},
Network: config.Network{
Wired: config.Wired{
DHCPEnabled: true,
IPSyncEnabled: true,
SharedStaticIP: true,
IPAddress: "",
SubnetMask: "",
DefaultGateway: "",
PrimaryDNS: "",
SecondaryDNS: "",
Authentication: "",
},
Wireless: config.Wireless{
Profiles: []string{},
},
},
Wireless: config.Wireless{
Authentication: config.Authentication{
Profiles: []string{},
},
},
Authentication: config.Authentication{
Profiles: []string{},
},
TLS: config.TLS{
MutualAuthentication: false,
Enabled: false,
TrustedCN: []string{},
},
Redirection: config.Redirection{
Enabled: true,
Services: config.Services{
KVM: true,
SOL: true,
IDER: true,
TLS: config.TLS{
MutualAuthentication: false,
Enabled: false,
TrustedCN: []string{},
},
UserConsent: "none",
},
UserAccounts: config.UserAccounts{
UserAccounts: []string{},
},
EnterpriseAssistant: config.EnterpriseAssistant{},
AMTSpecific: config.AMTSpecific{
ControlMode: "ccmactivate",
AdminPassword: "adminPassword",
ProvisioningCert: "",
ProvisioningCertPwd: "",
MEBXPassword: "",
Redirection: config.Redirection{
Enabled: true,
Services: config.Services{
KVM: true,
SOL: true,
IDER: true,
},
UserConsent: "none",
},
UserAccounts: config.UserAccounts{
UserAccounts: []string{},
},
EnterpriseAssistant: config.EnterpriseAssistant{},
AMTSpecific: config.AMTSpecific{
ControlMode: "ccmactivate",
AdminPassword: "adminPassword",
ProvisioningCert: "",
ProvisioningCertPwd: "",
MEBXPassword: "",
},
BMCSpecific: config.BMCSpecific{},
DASHSpecific: config.DASHSpecific{},
RedfishSpecific: config.RedfishSpecific{},
},
BMCSpecific: config.BMCSpecific{},
DASHSpecific: config.DASHSpecific{},
RedfishSpecific: config.RedfishSpecific{},
},
}
}
)

type expectedError struct {
Base64Error bool
Expand Down Expand Up @@ -129,6 +132,7 @@ func TestDecrypt(t *testing.T) {
t.Parallel()

var err error

var decryptedString []byte

cryptor := Crypto{}
Expand All @@ -152,7 +156,6 @@ func TestDecrypt(t *testing.T) {
assert.Equal(t, tc.message, string(decryptedString))
assert.NoError(t, err)
}

})
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/security/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (c Crypto) Encrypt(plainText []byte, key string) (string, error) {

func (c Crypto) GenerateKey() string {
key := make([]byte, 24) // 24 bytes will result in a 32-character base64 string

_, err := rand.Read(key)
if err != nil {
panic(err)
Expand Down
6 changes: 4 additions & 2 deletions pkg/security/encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ func TestEncrypt(t *testing.T) {
{
name: "successful encryption",
message: []byte("test message"),
key: string(validKey),
key: validKey,
expectedError: expectedError{},
errorMsg: nil,
expected: []byte("test message"),
},
{
name: "key too short",
message: []byte("test message"),
key: string(shortKey),
key: shortKey,
expectedError: expectedError{InvalidKeySizeError: true},
errorMsg: aes.KeySizeError(8),
expected: "",
Expand All @@ -42,6 +42,7 @@ func TestEncrypt(t *testing.T) {
t.Parallel()

var err error

var encryptedString string

cryptor := Crypto{}
Expand All @@ -54,6 +55,7 @@ func TestEncrypt(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, tc.expected, decryptedMessage)
}

if tc.expectedError.InvalidKeySizeError {
_, err = cryptor.Encrypt(tc.message, tc.key)
assert.Equal(t, tc.errorMsg, err)
Expand Down

0 comments on commit e91ff3e

Please sign in to comment.