Skip to content

Commit

Permalink
cmd: Support file:// format for TLS key material file flags in `opa…
Browse files Browse the repository at this point in the history
… run` (#7094)

Allowing Windows drive letters to be specified and respected.

Signed-off-by: Alex Rohozneanu <[email protected]>
  • Loading branch information
alexrohozneanu authored Oct 10, 2024
1 parent 588ae0d commit 20bb002
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/spf13/cobra"

"github.com/open-policy-agent/opa/cmd/internal/env"
fileurl "github.com/open-policy-agent/opa/internal/file/url"
"github.com/open-policy-agent/opa/runtime"
"github.com/open-policy-agent/opa/server"
"github.com/open-policy-agent/opa/util"
Expand Down Expand Up @@ -291,18 +292,31 @@ func initRuntime(ctx context.Context, params runCmdParams, args []string, addrSe
"1.3": tls.VersionTLS13,
}

cert, err := loadCertificate(params.tlsCertFile, params.tlsPrivateKeyFile)
tlsCertFilePath, err := fileurl.Clean(params.tlsCertFile)
if err != nil {
return nil, fmt.Errorf("invalid certificate file path: %w", err)
}
tlsPrivateKeyFilePath, err := fileurl.Clean(params.tlsPrivateKeyFile)
if err != nil {
return nil, fmt.Errorf("invalid certificate private key file path: %w", err)
}
tlsCACertFilePath, err := fileurl.Clean(params.tlsCACertFile)
if err != nil {
return nil, fmt.Errorf("invalid CA certificate file path: %w", err)
}

cert, err := loadCertificate(tlsCertFilePath, tlsPrivateKeyFilePath)
if err != nil {
return nil, err
}

params.rt.CertificateFile = params.tlsCertFile
params.rt.CertificateKeyFile = params.tlsPrivateKeyFile
params.rt.CertificateFile = tlsCertFilePath
params.rt.CertificateKeyFile = tlsPrivateKeyFilePath
params.rt.CertificateRefresh = params.tlsCertRefresh
params.rt.CertPoolFile = params.tlsCACertFile
params.rt.CertPoolFile = tlsCACertFilePath

if params.tlsCACertFile != "" {
pool, err := loadCertPool(params.tlsCACertFile)
if tlsCACertFilePath != "" {
pool, err := loadCertPool(tlsCACertFilePath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -422,7 +436,6 @@ func historyPath() string {
}

func loadCertificate(tlsCertFile, tlsPrivateKeyFile string) (*tls.Certificate, error) {

if tlsCertFile != "" && tlsPrivateKeyFile != "" {
cert, err := tls.LoadX509KeyPair(tlsCertFile, tlsPrivateKeyFile)
if err != nil {
Expand Down

0 comments on commit 20bb002

Please sign in to comment.