diff options
author | Andy Zhao <andyzhao@google.com> | 2023-02-08 09:35:03 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-08 09:35:03 -0800 |
commit | dc1cf9723817ec6254e81e349be8beb2a4bbeba7 (patch) | |
tree | fbc2144bdbb5466b6a2665884fe4634d064d9eb6 | |
parent | b022fa5bf44af935a4328ae8579a2bdb57f10ab7 (diff) | |
download | googleapis-enterprise-certificate-proxy-dc1cf9723817ec6254e81e349be8beb2a4bbeba7.tar.gz |
feat: Add ErrCredUnavailable sentinel error (#65)upstream/v0.2.2
-rw-r--r-- | client/client.go | 8 | ||||
-rw-r--r-- | client/client_test.go | 10 | ||||
-rw-r--r-- | client/testdata/certificate_config_missing_path.json | 9 | ||||
-rw-r--r-- | client/util/util.go | 9 |
4 files changed, 33 insertions, 3 deletions
diff --git a/client/client.go b/client/client.go index 80b3d2b..0a2d4d7 100644 --- a/client/client.go +++ b/client/client.go @@ -22,6 +22,7 @@ import ( "crypto/rsa" "crypto/x509" "encoding/gob" + "errors" "fmt" "io" "io/ioutil" @@ -119,6 +120,10 @@ func (k *Key) Sign(_ io.Reader, digest []byte, opts crypto.SignerOpts) (signed [ return } +// ErrCredUnavailable is a sentinel error that indicates ECP Cred is unavailable, +// possibly due to missing config or missing binary path. +var ErrCredUnavailable = errors.New("Cred is unavailable") + // Cred spawns a signer subprocess that listens on stdin/stdout to perform certificate // related operations, including signing messages with the private key. // @@ -133,6 +138,9 @@ func Cred(configFilePath string) (*Key, error) { } enterpriseCertSignerPath, err := util.LoadSignerBinaryPath(configFilePath) if err != nil { + if errors.Is(err, util.ErrConfigUnavailable) { + return nil, ErrCredUnavailable + } return nil, err } k := &Key{ diff --git a/client/client_test.go b/client/client_test.go index cbb7126..b0305b4 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -18,7 +18,6 @@ import ( "bytes" "crypto" "errors" - "os" "testing" ) @@ -31,11 +30,18 @@ func TestClient_Cred_Success(t *testing.T) { func TestClient_Cred_ConfigMissing(t *testing.T) { _, err := Cred("missing.json") - if got, want := err, os.ErrNotExist; !errors.Is(got, want) { + if got, want := err, ErrCredUnavailable; !errors.Is(got, want) { t.Errorf("Cred: with missing config; got %v, want %v err", got, want) } } +func TestClient_Cred_PathMissing(t *testing.T) { + _, err := Cred("testdata/certificate_config_missing_path.json") + if got, want := err, ErrCredUnavailable; !errors.Is(got, want) { + t.Errorf("Cred: with missing ECP path; got %v, want %v err", got, want) + } +} + func TestClient_Public(t *testing.T) { key, err := Cred("testdata/certificate_config.json") if err != nil { diff --git a/client/testdata/certificate_config_missing_path.json b/client/testdata/certificate_config_missing_path.json new file mode 100644 index 0000000..327d54c --- /dev/null +++ b/client/testdata/certificate_config_missing_path.json @@ -0,0 +1,9 @@ +{ + "cert_configs": { + "test": { + "issuer": "Test Issuer" + } + }, + "libs": { + } +} diff --git a/client/util/util.go b/client/util/util.go index 0226a6a..a88dbd4 100644 --- a/client/util/util.go +++ b/client/util/util.go @@ -36,10 +36,17 @@ type Libs struct { ECP string `json:"ecp"` } +// ErrConfigUnavailable is a sentinel error that indicates ECP config is unavailable, +// possibly due to entire config missing or missing binary path. +var ErrConfigUnavailable = errors.New("Config is unavailable") + // LoadSignerBinaryPath retrieves the path of the signer binary from the config file. func LoadSignerBinaryPath(configFilePath string) (path string, err error) { jsonFile, err := os.Open(configFilePath) if err != nil { + if errors.Is(err, os.ErrNotExist) { + return "", ErrConfigUnavailable + } return "", err } @@ -54,7 +61,7 @@ func LoadSignerBinaryPath(configFilePath string) (path string, err error) { } signerBinaryPath := config.Libs.ECP if signerBinaryPath == "" { - return "", errors.New("signer binary path is missing") + return "", ErrConfigUnavailable } return signerBinaryPath, nil } |