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: Ralf Pannemans <[email protected]>
  • Loading branch information
modulo11 and c0d1ngm0nk3y committed Dec 11, 2023
1 parent 85c08cf commit 179c5cb
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 @@ -29,7 +29,6 @@ import (

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

const DefaultCertFile = "/etc/ssl/certs/ca-certificates.crt"
Expand Down Expand Up @@ -59,10 +58,6 @@ func NewCertificateLoader(logger log.Logger) 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 179c5cb

Please sign in to comment.