Skip to content

Commit

Permalink
Refactor: remove PemReader interface and simplify usage
Browse files Browse the repository at this point in the history
Removed the PemReader interface and associated methods to streamline the code. Function calls have been updated to directly use the ParseFileOrPath function without needing a PemReader instance. This simplifies the codebase and reduces redundancy.
  • Loading branch information
rolandgroen committed Oct 14, 2024
1 parent aa4ca20 commit 3d993b0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 30 deletions.
16 changes: 1 addition & 15 deletions pem/pem_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,8 @@ import (
"os"
)

// PemReader defines the interface for parsing PEM-encoded files from a given path.
type PemReader interface {
ParseFileOrPath(path string, pemType string) (*[][]byte, error)
}

// DefaultPemReader handles reading and parsing of PEM files or directories containing PEM files.
type DefaultPemReader struct {
}

// NewPemReader creates and returns a new instance of DefaultPemReader.
func NewPemReader() *DefaultPemReader {
return &DefaultPemReader{}
}

// ParseFileOrPath processes a file or directory at the given path and extracts PEM blocks of the specified pemType.
func (p *DefaultPemReader) ParseFileOrPath(path string, pemType string) (*[][]byte, error) {
func ParseFileOrPath(path string, pemType string) (*[][]byte, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return nil, err
Expand Down
18 changes: 6 additions & 12 deletions pem/pem_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ func TestParseFileOrPath(t *testing.T) {
pemType := "CERTIFICATE"

t.Run("FileExistsAndIsNotDirectory", func(t *testing.T) {
pemReader := NewPemReader()
result, err := pemReader.ParseFileOrPath(tempFile.Name(), pemType)
result, err := ParseFileOrPath(tempFile.Name(), pemType)
assert.NoError(t, err)
assert.NotNil(t, result)
})

t.Run("FileDoesNotExist", func(t *testing.T) {
pemReader := NewPemReader()
_, err := pemReader.ParseFileOrPath("nonexistent", pemType)
_, err := ParseFileOrPath("nonexistent", pemType)
assert.Error(t, err)
})

Expand All @@ -42,18 +40,15 @@ func TestParseFileOrPath(t *testing.T) {
}(tempDir)

t.Run("PathIsDirectory", func(t *testing.T) {
pemReader := NewPemReader()
_, err := pemReader.ParseFileOrPath(tempDir, pemType)
_, err := ParseFileOrPath(tempDir, pemType)
assert.NoError(t, err)
})

t.Run("PathDoesNotExist", func(t *testing.T) {
pemReader := NewPemReader()
_, err := pemReader.ParseFileOrPath("nonexistent/path", pemType)
_, err := ParseFileOrPath("nonexistent/path", pemType)
assert.Error(t, err)
})
t.Run("Happy flow single file", func(t *testing.T) {
pemReader := NewPemReader()
file, err := os.CreateTemp(tempDir, "prefix")
if err != nil {
t.Fatal(err)
Expand All @@ -78,7 +73,7 @@ func TestParseFileOrPath(t *testing.T) {
t.Fail()
}
}
data, err := pemReader.ParseFileOrPath(file.Name(), pemType)
data, err := ParseFileOrPath(file.Name(), pemType)
assert.NoError(t, err)
for i := 0; i < len(*data); i++ {
bytes := (*data)[i]
Expand All @@ -91,7 +86,6 @@ func TestParseFileOrPath(t *testing.T) {

})
t.Run("Happy flow directory", func(t *testing.T) {
pemReader := NewPemReader()
certs, chainPem, _, _, _, err := x509_cert.BuildCertChain("A BIG STRING")
assert.NoError(t, err)
tempDir, _ := os.MkdirTemp("", "example")
Expand All @@ -113,7 +107,7 @@ func TestParseFileOrPath(t *testing.T) {
t.Fail()
}
}
data, err := pemReader.ParseFileOrPath(tempDir, pemType)
data, err := ParseFileOrPath(tempDir, pemType)
assert.NoError(t, err)
dataMap := make(map[string][]byte)
for i := 0; i < len(*data); i++ {
Expand Down
5 changes: 2 additions & 3 deletions uzi_vc_issuer/ura_issuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ func NewUraVcBuilder(chainParser x509_cert.ChainParser) *DefaultUraIssuer {

// Issue generates a URA Verifiable Credential using provided certificate, signing key, subject DID, and subject name.
func (u DefaultUraIssuer) Issue(certificateFile string, signingKeyFile string, subjectDID string, test bool) (string, error) {
reader := pem2.NewPemReader()
certificate, err := reader.ParseFileOrPath(certificateFile, "CERTIFICATE")
certificate, err := pem2.ParseFileOrPath(certificateFile, "CERTIFICATE")
if err != nil {
return "", err
}
Expand All @@ -68,7 +67,7 @@ func (u DefaultUraIssuer) Issue(certificateFile string, signingKeyFile string, s
_chain := append(*chain, *certificate...)
chain = &_chain

signingKeys, err := reader.ParseFileOrPath(signingKeyFile, "PRIVATE KEY")
signingKeys, err := pem2.ParseFileOrPath(signingKeyFile, "PRIVATE KEY")
if err != nil {
return "", err
}
Expand Down

0 comments on commit 3d993b0

Please sign in to comment.