-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move CTL logging logic over to CTL package (#353)
* Refacter CT log client to use interface Signed-off-by: Nathan Smith <nathan@nfsmith.ca> * Add logging middleware for CTL client Signed-off-by: Nathan Smith <nathan@nfsmith.ca> * Log in CTL client not API handler Removes CTL related logging from the API handler and pushes it over to the CTL client. Signed-off-by: Nathan Smith <nathan@nfsmith.ca>
- Loading branch information
Showing
8 changed files
with
184 additions
and
24 deletions.
There are no files selected for viewing
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
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
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
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
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,44 @@ | ||
// Copyright 2021 The Sigstore Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package ctl | ||
|
||
import ( | ||
"github.com/sigstore/fulcio/pkg/ca" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type ctlLoggingClient struct { | ||
next Client | ||
logger *zap.SugaredLogger | ||
} | ||
|
||
func (lc *ctlLoggingClient) AddChain(csc *ca.CodeSigningCertificate) (*CertChainResponse, error) { | ||
lc.logger.Info("Submitting CTL inclusion for subject", csc.Subject.Value) | ||
resp, err := lc.next.AddChain(csc) | ||
if err != nil { | ||
lc.logger.Error("Failed to submit certificate to CTL with error", err) | ||
return nil, err | ||
} | ||
lc.logger.Info("CTL Submission Signature Received: ", resp.Signature) | ||
lc.logger.Info("CTL Submission ID Received: ", resp.ID) | ||
return resp, nil | ||
} | ||
|
||
// WithLogging adds logging (in the writing helpful information to console | ||
// sense) to a certificate transparenct log client | ||
func WithLogging(next Client, logger *zap.SugaredLogger) Client { | ||
return &ctlLoggingClient{next, logger} | ||
} |
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,78 @@ | ||
// Copyright 2021 The Sigstore Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ctl | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/sigstore/fulcio/pkg/ca" | ||
"github.com/sigstore/fulcio/pkg/challenges" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zaptest/observer" | ||
) | ||
|
||
type clientFunc func(csc *ca.CodeSigningCertificate) (*CertChainResponse, error) | ||
|
||
func (f clientFunc) AddChain(csc *ca.CodeSigningCertificate) (*CertChainResponse, error) { | ||
return f(csc) | ||
} | ||
|
||
func TestWithLogging(t *testing.T) { | ||
tests := map[string]struct { | ||
Client Client | ||
ExpectedOutput *regexp.Regexp | ||
}{ | ||
"Error in client should be logged": { | ||
clientFunc(func(*ca.CodeSigningCertificate) (*CertChainResponse, error) { | ||
return nil, errors.New(`ctl: testing error`) | ||
}), | ||
regexp.MustCompile(`ctl: testing error`), | ||
}, | ||
"Success in client should log information": { | ||
clientFunc(func(*ca.CodeSigningCertificate) (*CertChainResponse, error) { | ||
return &CertChainResponse{}, nil | ||
}), | ||
regexp.MustCompile(`CTL Submission ID Received:`), | ||
}, | ||
} | ||
|
||
for test, data := range tests { | ||
t.Run(test, func(t *testing.T) { | ||
// Given | ||
observedZapCore, observedLogs := observer.New(zap.InfoLevel) | ||
observedLogger := zap.New(observedZapCore) | ||
client := WithLogging(data.Client, observedLogger.Sugar()) | ||
|
||
csc := ca.CodeSigningCertificate{ | ||
Subject: &challenges.ChallengeResult{}, | ||
} | ||
|
||
// When | ||
_, _ = client.AddChain(&csc) | ||
|
||
// Then | ||
for _, entry := range observedLogs.All() { | ||
if data.ExpectedOutput.MatchString(entry.Message) { | ||
// We received expected output so the test passes | ||
return | ||
} | ||
} | ||
// If we got here we didn't match the expected output so test fails | ||
t.Error("Didn't find expected output in logs") | ||
}) | ||
} | ||
} |
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,31 @@ | ||
// Copyright 2021 The Sigstore Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package ctl | ||
|
||
import "fmt" | ||
|
||
type ErrorResponse struct { | ||
StatusCode int `json:"statusCode"` | ||
ErrorCode string `json:"errorCode"` | ||
Message string `json:"message"` | ||
} | ||
|
||
func (err *ErrorResponse) Error() string { | ||
if err.ErrorCode == "" { | ||
return fmt.Sprintf("%d CT API error: %s", err.StatusCode, err.Message) | ||
} | ||
return fmt.Sprintf("%d (%s) CT API error: %s", err.StatusCode, err.ErrorCode, err.Message) | ||
} |
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,22 @@ | ||
// Copyright 2021 The Sigstore Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package ctl | ||
|
||
import "github.com/sigstore/fulcio/pkg/ca" | ||
|
||
type Client interface { | ||
AddChain(csc *ca.CodeSigningCertificate) (*CertChainResponse, error) | ||
} |