jx/internal/resource/pki_test.go

186 lines
6.8 KiB
Go

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package resource
import (
"context"
_ "encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
_ "gopkg.in/yaml.v3"
"io"
_ "log"
_ "os"
"decl/internal/transport"
"decl/internal/codec"
"decl/internal/data"
"decl/internal/folio"
"gitea.rosskeen.house/rosskeen.house/machine"
"strings"
"testing"
"path/filepath"
)
type StringContentReadWriter func() (any, error)
func (s StringContentReadWriter) ContentWriterStream() (*transport.Writer, error) {
w, e := s()
return w.(*transport.Writer), e
}
func (s StringContentReadWriter) ContentReaderStream() (*transport.Reader, error) {
r, e := s()
return r.(*transport.Reader), e
}
func (s StringContentReadWriter) Apply(string) error { return nil }
func (s StringContentReadWriter) Clone() data.Declaration { return nil }
func (s StringContentReadWriter) Load(docData []byte, f codec.Format) (err error) { return }
func (s StringContentReadWriter) LoadReader(r io.ReadCloser, f codec.Format) (err error) { return }
func (s StringContentReadWriter) LoadString(docData string, f codec.Format) (err error) { return }
func (s StringContentReadWriter) LoadDecl(yamlResourceDeclaration string) error { return nil }
func (s StringContentReadWriter) ResolveId(ctx context.Context) (string) { return "" }
func (s StringContentReadWriter) SetURI(uri string) (error) { return nil }
func (s StringContentReadWriter) SetParsedURI(uri data.URIParser) (error) { return nil }
func (s StringContentReadWriter) URI() (string) { return "" }
func (s StringContentReadWriter) Validate() (error) { return nil }
func (s StringContentReadWriter) ResourceType() data.TypeName { return "" }
func (s StringContentReadWriter) Resource() data.Resource { return stringContentReadWriterResource { StringContentReadWriter: s } }
type stringContentReadWriterResource struct {
StringContentReadWriter
}
//URI() string { return "" }
//SetParsedURI(URIParser) error { return nil }
func (s stringContentReadWriterResource) Type() string { return "buffer" }
func (s stringContentReadWriterResource) StateMachine() machine.Stater { return nil }
func (s stringContentReadWriterResource) UseConfig(config data.ConfigurationValueGetter) { return }
//ResolveId(context.Context) string { return }
//LoadString(string, codec.Format) (error) { return }
//Load([]byte, codec.Format) (error) { return }
//LoadReader(io.ReadCloser, codec.Format) (error) { return }
func (s stringContentReadWriterResource) Apply() error { return nil }
func (s stringContentReadWriterResource) Create(context.Context) error { return nil }
func (s stringContentReadWriterResource) Read(context.Context) ([]byte, error) { return nil, nil }
func (s stringContentReadWriterResource) Update(context.Context) error { return nil }
func (s stringContentReadWriterResource) Delete(context.Context) error { return nil }
//Validate() error { return }
func (s stringContentReadWriterResource) Clone() data.Resource { return nil }
func (s stringContentReadWriterResource) SetResourceMapper(folio.ResourceMapper) { return }
func TestNewPKIKeysResource(t *testing.T) {
r := NewPKI()
assert.NotNil(t, r)
}
func TestPKIPrivateKey(t *testing.T) {
r := NewPKI()
assert.NotNil(t, r)
assert.Equal(t, 2048, r.Bits)
assert.Nil(t, r.GenerateKey())
assert.NotNil(t, r.privateKey)
r.PublicKey()
assert.NotNil(t, r.publicKey)
}
func TestPKIEncodeKeys(t *testing.T) {
var privateTarget, publicTarget, certTarget strings.Builder
r := NewPKI()
assert.NotNil(t, r)
assert.Equal(t, 2048, r.Bits)
assert.Nil(t, r.GenerateKey())
assert.NotNil(t, r.privateKey)
r.PublicKey()
assert.NotNil(t, r.publicKey)
r.Resources = folio.NewResourceMapper()
privateKeyBuffer := folio.NewDeclaration()
privateKeyBuffer.Attributes = NewMockBufferResource("privatekey", &privateTarget)
r.Resources.Set("buffer://privatekey", privateKeyBuffer)
publicKeyBuffer := folio.NewDeclaration()
publicKeyBuffer.Attributes = NewMockBufferResource("publickey", &publicTarget)
r.Resources.Set("buffer://publickey", publicKeyBuffer)
certKeyBuffer := folio.NewDeclaration()
certKeyBuffer.Attributes = NewMockBufferResource("certificate", &certTarget)
r.Resources.Set("buffer://certificate", certKeyBuffer)
r.PrivateKeyRef = folio.ResourceReference("buffer://privatekey")
r.PublicKeyRef = folio.ResourceReference("buffer://publickey")
r.Encode()
assert.Equal(t, "-----BEGIN RSA PRIVATE KEY-----", strings.Split(privateTarget.String(), "\n")[0])
assert.Equal(t, "-----BEGIN RSA PUBLIC KEY-----", strings.Split(publicTarget.String(), "\n")[0])
r.CertificateRef = folio.ResourceReference("buffer://certificate")
e := r.GenerateCertificate()
assert.Nil(t, e)
assert.Equal(t, "-----BEGIN CERTIFICATE-----", strings.Split(certTarget.String(), "\n")[0])
}
func TestPKIResource(t *testing.T) {
privateKeyFile, _ := filepath.Abs(TempDir.FilePath("fooprivatekey.pem"))
publicKeyFile, _ := filepath.Abs(TempDir.FilePath("foopublickey.pem"))
certFile, _ := filepath.Abs(TempDir.FilePath("foocert.pem"))
var resourceYaml, readResourceYaml strings.Builder
expected := fmt.Sprintf(`
privatekeyref: "file://%s"
publickeyref: "file://%s"
certificateref: "file://%s"
bits: 2048
type: pem
`, privateKeyFile, publicKeyFile, certFile)
r := NewPKI()
assert.NotNil(t, r)
assert.Equal(t, 2048, r.Bits)
assert.Nil(t, r.GenerateKey())
assert.NotNil(t, r.privateKey)
r.PublicKey()
assert.NotNil(t, r.publicKey)
r.PrivateKeyRef = folio.ResourceReference(fmt.Sprintf("file://%s", privateKeyFile))
r.PublicKeyRef = folio.ResourceReference(fmt.Sprintf("file://%s", publicKeyFile))
r.CertificateRef = folio.ResourceReference(fmt.Sprintf("file://%s", certFile))
createErr := r.Create(context.Background())
assert.Nil(t, createErr)
assert.FileExists(t, privateKeyFile)
assert.FileExists(t, publicKeyFile)
assert.FileExists(t, certFile)
serializeErr := codec.FormatYaml.Serialize(r, &resourceYaml)
assert.Nil(t, serializeErr)
assert.YAMLEq(t, expected, resourceYaml.String())
read := NewPKI()
assert.NotNil(t, read)
read.PrivateKeyRef = folio.ResourceReference(fmt.Sprintf("file://%s", privateKeyFile))
read.PublicKeyRef = folio.ResourceReference(fmt.Sprintf("file://%s", publicKeyFile))
read.CertificateRef = folio.ResourceReference(fmt.Sprintf("file://%s", certFile))
_, readErr := read.Read(context.Background())
assert.Nil(t, readErr)
expectedContent := fmt.Sprintf(`
privatekey: |
%s
publickey: |
%s
certificate: |
%s
privatekeyref: "file://%s"
publickeyref: "file://%s"
certificateref: "file://%s"
bits: 2048
type: pem
`, strings.Replace(read.PrivateKeyPem, "\n", "\n ", -1), strings.Replace(read.PublicKeyPem, "\n", "\n ", -1), strings.Replace(read.CertificatePem, "\n", "\n ", -1), privateKeyFile, publicKeyFile, certFile)
readSerializeErr := codec.FormatYaml.Serialize(read, &readResourceYaml)
assert.Nil(t, readSerializeErr)
assert.YAMLEq(t, expectedContent, readResourceYaml.String())
}