-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
53 additions
and
14 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
runtime-watcher/internal/cacertificatehandler/ca_certificate_handler.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package cacertificatehandler | ||
|
||
import ( | ||
"crypto/x509" | ||
"encoding/pem" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/go-logr/logr" | ||
) | ||
|
||
// TODO: Remove logger after debugging | ||
func GetCertificatePool(certPath string, logger logr.Logger) (*x509.CertPool, error) { | ||
certBytes, err := getCertBytes(certPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
logger.Info("Certificate bytes: " + string(certBytes)) | ||
|
||
certificate, err := parseCertificate(certBytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
logger.Info("Certificate: " + certificate.SerialNumber.String()) | ||
rootCertPool := x509.NewCertPool() | ||
rootCertPool.AddCert(certificate) | ||
return rootCertPool, nil | ||
} | ||
|
||
func getCertBytes(certPath string) ([]byte, error) { | ||
certBytes, err := os.ReadFile(certPath) | ||
if err != nil { | ||
msg := "could not load CA certificate" | ||
return nil, fmt.Errorf("%s :%w", msg, err) | ||
} | ||
|
||
return certBytes, nil | ||
} | ||
|
||
func parseCertificate(certBytes []byte) (*x509.Certificate, error) { | ||
publicPemBlock, _ := pem.Decode(certBytes) | ||
rootPubCrt, errParse := x509.ParseCertificate(publicPemBlock.Bytes) | ||
if errParse != nil { | ||
msg := "failed to parse public key" | ||
return nil, fmt.Errorf("%s :%w", msg, errParse) | ||
} | ||
|
||
return rootPubCrt, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters