Skip to content

Commit

Permalink
check write permission just before write to keystore
Browse files Browse the repository at this point in the history
Co-authored-by: Johannes Dillmann <[email protected]>
  • Loading branch information
c0d1ngm0nk3y and modulo11 committed Jan 10, 2024
1 parent ffe3121 commit f25b563
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
5 changes: 0 additions & 5 deletions certificate_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"time"

"github.com/paketo-buildpacks/libpak/sherpa"
"golang.org/x/sys/unix"
)

const DefaultCertFile = "/etc/ssl/certs/ca-certificates.crt"
Expand Down Expand Up @@ -56,10 +55,6 @@ func NewCertificateLoader() CertificateLoader {
}

func (c *CertificateLoader) Load(path string, password string) error {
if unix.Access(path, unix.W_OK) != nil {
return nil
}

ks, err := DetectKeystore(path)
if err != nil {
return err
Expand Down
9 changes: 9 additions & 0 deletions keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"

"github.com/pavlo-v-chernykh/keystore-go/v4"
"golang.org/x/sys/unix"
"software.sslmate.com/src/go-pkcs12"
)

Expand Down Expand Up @@ -90,6 +91,10 @@ func (k *JKSKeystore) Add(name string, b *pem.Block) error {
}

func (k *JKSKeystore) Write() error {
if unix.Access(k.location, unix.W_OK) != nil {
return nil
}

out, err := os.OpenFile(k.location, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("unable to open %s\n%w", k.location, err)
Expand Down Expand Up @@ -154,6 +159,10 @@ func (k *PasswordLessPKCS12Keystore) Add(name string, b *pem.Block) error {
}

func (k *PasswordLessPKCS12Keystore) Write() error {
if unix.Access(k.location, unix.W_OK) != nil {
return nil
}

out, err := os.OpenFile(k.location, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return err
Expand Down

0 comments on commit f25b563

Please sign in to comment.