Skip to content

Commit

Permalink
fix: update
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao committed Sep 14, 2024
1 parent deec55e commit c6132af
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 191 deletions.
17 changes: 8 additions & 9 deletions revocation/internal/crl/crl.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"net/url"
"time"

"github.com/notaryproject/notation-core-go/revocation/internal/revocation"
"github.com/notaryproject/notation-core-go/revocation/result"
)

Expand Down Expand Up @@ -73,10 +72,10 @@ func CertCheckStatus(ctx context.Context, cert, issuer *x509.Certificate, opts C
return &result.CertRevocationResult{
Result: result.ResultNonRevokable,
ServerResults: []*result.ServerResult{{
RevocationMethod: revocation.MethodCRL,
RevocationMethod: result.RevocationMethodCRL,
Result: result.ResultNonRevokable,
}},
RevocationMethod: revocation.MethodCRL,
RevocationMethod: result.RevocationMethodCRL,
}
}

Expand Down Expand Up @@ -113,7 +112,7 @@ func CertCheckStatus(ctx context.Context, cert, issuer *x509.Certificate, opts C
return &result.CertRevocationResult{
Result: result.ResultRevoked,
ServerResults: []*result.ServerResult{crlResult},
RevocationMethod: revocation.MethodCRL,
RevocationMethod: result.RevocationMethodCRL,
}
}

Expand All @@ -128,16 +127,16 @@ func CertCheckStatus(ctx context.Context, cert, issuer *x509.Certificate, opts C
Result: result.ResultUnknown,
Server: crlURL,
Error: lastErr,
RevocationMethod: revocation.MethodCRL,
RevocationMethod: result.RevocationMethodCRL,
}},
RevocationMethod: revocation.MethodCRL,
RevocationMethod: result.RevocationMethodCRL,
}
}

return &result.CertRevocationResult{
Result: result.ResultOK,
ServerResults: serverResults,
RevocationMethod: revocation.MethodCRL,
RevocationMethod: result.RevocationMethodCRL,
}
}

Expand Down Expand Up @@ -206,15 +205,15 @@ func checkRevocation(cert *x509.Certificate, baseCRL *x509.RevocationList, signi
return &result.ServerResult{
Result: result.ResultRevoked,
Server: crlURL,
RevocationMethod: revocation.MethodCRL,
RevocationMethod: result.RevocationMethodCRL,
}, nil
}
}

return &result.ServerResult{
Result: result.ResultOK,
Server: crlURL,
RevocationMethod: revocation.MethodCRL,
RevocationMethod: result.RevocationMethodCRL,
}, nil
}

Expand Down
7 changes: 3 additions & 4 deletions revocation/internal/ocsp/ocsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"strings"
"time"

"github.com/notaryproject/notation-core-go/revocation/internal/revocation"
"github.com/notaryproject/notation-core-go/revocation/result"
"golang.org/x/crypto/ocsp"
)
Expand Down Expand Up @@ -59,7 +58,7 @@ func CertCheckStatus(cert, issuer *x509.Certificate, opts CertCheckStatusOptions
return &result.CertRevocationResult{
Result: result.ResultNonRevokable,
ServerResults: []*result.ServerResult{toServerResult("", NoServerError{})},
RevocationMethod: revocation.MethodOCSP,
RevocationMethod: result.RevocationMethodOCSP,
}
}
ocspURLs := cert.OCSPServer
Expand Down Expand Up @@ -230,14 +229,14 @@ func toServerResult(server string, err error) *result.ServerResult {
// and TimeoutError
serverResult = result.NewServerResult(result.ResultUnknown, server, t)
}
serverResult.RevocationMethod = revocation.MethodOCSP
serverResult.RevocationMethod = result.RevocationMethodOCSP
return serverResult
}

func serverResultsToCertRevocationResult(serverResults []*result.ServerResult) *result.CertRevocationResult {
return &result.CertRevocationResult{
Result: serverResults[len(serverResults)-1].Result,
ServerResults: serverResults,
RevocationMethod: revocation.MethodOCSP,
RevocationMethod: result.RevocationMethodOCSP,
}
}
53 changes: 0 additions & 53 deletions revocation/internal/revocation/method.go

This file was deleted.

37 changes: 0 additions & 37 deletions revocation/internal/revocation/method_test.go

This file was deleted.

41 changes: 0 additions & 41 deletions revocation/method.go

This file was deleted.

47 changes: 40 additions & 7 deletions revocation/result/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
// Package result provides general objects that are used across revocation
package result

import (
"strconv"

"github.com/notaryproject/notation-core-go/revocation/internal/revocation"
)
import "strconv"

// Result is a type of enumerated value to help characterize revocation result.
// It can be OK, Unknown, NonRevokable, or Revoked
Expand Down Expand Up @@ -60,6 +56,43 @@ func (r Result) String() string {
}
}

// RevocationMethod defines the method used to check the revocation status of a
// certificate.
type RevocationMethod int

const (
// RevocationMethodUnknown is used for root certificates or when the method
// used to check the revocation status of a certificate is unknown.
RevocationMethodUnknown RevocationMethod = iota

// RevocationMethodOCSP represents OCSP as the method used to check the
// revocation status of a certificate.
RevocationMethodOCSP

// RevocationMethodCRL represents CRL as the method used to check the
// revocation status of a certificate.
RevocationMethodCRL

// RevocationMethodOCSPFallbackCRL represents OCSP check with unknown error
// fallback to CRL as the method used to check the revocation status of a
// certificate.
RevocationMethodOCSPFallbackCRL
)

// String provides a conversion from a Method to a string
func (m RevocationMethod) String() string {
switch m {
case RevocationMethodOCSP:
return "OCSP"
case RevocationMethodCRL:
return "CRL"
case RevocationMethodOCSPFallbackCRL:
return "OCSPFallbackCRL"
default:
return "Unknown"
}
}

// ServerResult encapsulates the OCSP result for a single server or the CRL
// result for a single CRL URI for a certificate in the chain
type ServerResult struct {
Expand All @@ -78,7 +111,7 @@ type ServerResult struct {

// RevocationMethod is the method used to check the revocation status of the
// certificate, including MethodUnknown, MethodOCSP, MethodCRL
RevocationMethod revocation.Method
RevocationMethod RevocationMethod
}

// NewServerResult creates a ServerResult object from its individual parts: a
Expand Down Expand Up @@ -121,5 +154,5 @@ type CertRevocationResult struct {
// RevocationMethod is the method used to check the revocation status of the
// certificate, including MethodUnknown, MethodOCSP, MethodCRL and
// MethodOCSPFallbackCRL
RevocationMethod revocation.Method
RevocationMethod RevocationMethod
}
21 changes: 21 additions & 0 deletions revocation/result/results_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ func TestResultString(t *testing.T) {
})
}

func TestMethodString(t *testing.T) {
tests := []struct {
method RevocationMethod
expected string
}{
{RevocationMethodOCSP, "OCSP"},
{RevocationMethodCRL, "CRL"},
{RevocationMethodOCSPFallbackCRL, "OCSPFallbackCRL"},
{RevocationMethod(999), "Unknown"}, // Test for default case
}

for _, tt := range tests {
t.Run(tt.expected, func(t *testing.T) {
result := tt.method.String()
if result != tt.expected {
t.Errorf("expected %s, got %s", tt.expected, result)
}
})
}
}

func TestNewServerResult(t *testing.T) {
expectedR := &ServerResult{
Result: ResultNonRevokable,
Expand Down
18 changes: 9 additions & 9 deletions revocation/revocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ func (r *revocation) ValidateContext(ctx context.Context, validateContextOpts Va

ocspResult := ocsp.CertCheckStatus(cert, certChain[i+1], ocspOpts)
if ocspResult != nil && ocspResult.Result == result.ResultUnknown && crl.Supported(cert) {
// try CRL check if OCSP result is unknown
result := crl.CertCheckStatus(ctx, cert, certChain[i+1], crlOpts)
// try CRL check if OCSP serverResult is unknown
serverResult := crl.CertCheckStatus(ctx, cert, certChain[i+1], crlOpts)
// append CRL result to OCSP result
result.ServerResults = append(ocspResult.ServerResults, result.ServerResults...)
result.RevocationMethod = MethodOCSPFallbackCRL
certResults[i] = result
serverResult.ServerResults = append(ocspResult.ServerResults, serverResult.ServerResults...)
serverResult.RevocationMethod = result.RevocationMethodOCSPFallbackCRL
certResults[i] = serverResult
} else {
certResults[i] = ocspResult
}
Expand All @@ -230,9 +230,9 @@ func (r *revocation) ValidateContext(ctx context.Context, validateContextOpts Va
Result: result.ResultNonRevokable,
ServerResults: []*result.ServerResult{{
Result: result.ResultNonRevokable,
RevocationMethod: MethodUnknown,
RevocationMethod: result.RevocationMethodUnknown,
}},
RevocationMethod: MethodUnknown,
RevocationMethod: result.RevocationMethodUnknown,
}
}
}
Expand All @@ -242,9 +242,9 @@ func (r *revocation) ValidateContext(ctx context.Context, validateContextOpts Va
Result: result.ResultNonRevokable,
ServerResults: []*result.ServerResult{{
Result: result.ResultNonRevokable,
RevocationMethod: MethodUnknown,
RevocationMethod: result.RevocationMethodUnknown,
}},
RevocationMethod: MethodUnknown,
RevocationMethod: result.RevocationMethodUnknown,
}
wg.Wait()

Expand Down
Loading

0 comments on commit c6132af

Please sign in to comment.