forked from notaryproject/notation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Patrick Zheng <[email protected]>
- Loading branch information
1 parent
b2d5fac
commit 17a0585
Showing
6 changed files
with
147 additions
and
51 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,60 @@ | ||
// Copyright The Notary Project 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 revocation | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/notaryproject/notation-core-go/revocation" | ||
corecrl "github.com/notaryproject/notation-core-go/revocation/crl" | ||
"github.com/notaryproject/notation-core-go/revocation/purpose" | ||
"github.com/notaryproject/notation-go/dir" | ||
"github.com/notaryproject/notation-go/verifier/crl" | ||
clicrl "github.com/notaryproject/notation/internal/crl" | ||
"github.com/notaryproject/notation/internal/httputil" | ||
) | ||
|
||
// NewRevocationValidator returns a revocation.Validator given the certificate | ||
// purpose | ||
func NewRevocationValidator(ctx context.Context, purpose purpose.Purpose) (revocation.Validator, error) { | ||
ocspHttpClient := httputil.NewClient(ctx, &http.Client{Timeout: 2 * time.Second}) | ||
crlFetcher, err := corecrl.NewHTTPFetcher(httputil.NewClient(ctx, &http.Client{Timeout: 5 * time.Second})) | ||
if err != nil { | ||
return nil, err | ||
} | ||
crlFetcher.DiscardCacheError = true // discard crl cache error | ||
cacheRoot, err := dir.CacheFS().SysPath(dir.PathCRLCache) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fileCache, err := crl.NewFileCache(cacheRoot) | ||
if err != nil { | ||
// discard NewFileCache error as cache errors are not critical | ||
fmt.Fprintf(os.Stderr, "Warning: %v\n", err) | ||
} else { | ||
crlFetcher.Cache = &clicrl.CacheWithLog{ | ||
Cache: fileCache, | ||
DiscardCacheError: crlFetcher.DiscardCacheError, | ||
} | ||
} | ||
return revocation.NewWithOptions(revocation.Options{ | ||
OCSPHTTPClient: ocspHttpClient, | ||
CRLFetcher: crlFetcher, | ||
CertChainPurpose: purpose, | ||
}) | ||
} |
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,59 @@ | ||
// Copyright The Notary Project 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 revocation | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/notaryproject/notation-core-go/revocation/purpose" | ||
"github.com/notaryproject/notation-go/dir" | ||
) | ||
|
||
func TestNewRevocationValidator(t *testing.T) { | ||
defer func(oldCacheDir string) { | ||
dir.UserCacheDir = oldCacheDir | ||
}(dir.UserCacheDir) | ||
|
||
t.Run("Success", func(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
t.Skip("skipping test on Windows") | ||
} | ||
if _, err := NewRevocationValidator(context.Background(), purpose.Timestamping); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
|
||
tempRoot := t.TempDir() | ||
t.Run("Success but without permission to create cache directory", func(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
t.Skip("skipping test on Windows") | ||
} | ||
dir.UserCacheDir = tempRoot | ||
if err := os.Chmod(tempRoot, 0); err != nil { | ||
t.Fatal(err) | ||
} | ||
defer func() { | ||
// restore permission | ||
if err := os.Chmod(tempRoot, 0755); err != nil { | ||
t.Fatalf("failed to change permission: %v", err) | ||
} | ||
}() | ||
if _, err := NewRevocationValidator(context.Background(), purpose.Timestamping); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
} |