diff --git a/pem/pem_reader.go b/pem/pem_reader.go index f6c2e31..03e0db7 100644 --- a/pem/pem_reader.go +++ b/pem/pem_reader.go @@ -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 diff --git a/pem/pem_reader_test.go b/pem/pem_reader_test.go index e71c1ee..a46d8bb 100644 --- a/pem/pem_reader_test.go +++ b/pem/pem_reader_test.go @@ -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) }) @@ -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) @@ -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] @@ -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") @@ -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++ { diff --git a/uzi_vc_issuer/ura_issuer.go b/uzi_vc_issuer/ura_issuer.go index ff8530f..c3d32bf 100644 --- a/uzi_vc_issuer/ura_issuer.go +++ b/uzi_vc_issuer/ura_issuer.go @@ -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 } @@ -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 }