-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Enable Witness Policy verify from Archivista
Enables Witness to retrieve Policy from Archivista. If a user provides a Policy `gitoid`, Witness will attempt to retrieve it from Archivista. To maintain backward compatibility, Witness first tries to load the file locally. If the local file loading fails and an Archivista is configured, it will retrieve the Policy from Archivista. Signed-off-by: Kairo Araujo <kairo.araujo@testifysec.com>
- Loading branch information
1 parent
fb15191
commit 00d38f9
Showing
2 changed files
with
74 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2024 The Witness Contributors | ||
// | ||
// 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 pkg | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/in-toto/go-witness/archivista" | ||
"github.com/in-toto/go-witness/dsse" | ||
"github.com/in-toto/go-witness/log" | ||
) | ||
|
||
// Load policy from a file or Archivista | ||
// | ||
// It prefers to load from a file, if it fails, it tries to load from Archivista | ||
func LoadPolicy(ctx context.Context, policy string, archivista *archivista.Client) (dsse.Envelope, error) { | ||
policyEnvelope := dsse.Envelope{} | ||
|
||
filePolicy, err := os.Open(policy) | ||
if err != nil { | ||
log.Debug("failed to open policy file: ", policy) | ||
if archivista == nil { | ||
return policyEnvelope, fmt.Errorf("failed to open file to sign: %w", err) | ||
} else { | ||
log.Debug("attempting to fetch policy " + policy + " from archivista") | ||
policyEnvelope, err = archivista.Download(ctx, policy) | ||
if err != nil { | ||
return policyEnvelope, fmt.Errorf("failed to fetch policy from archivista: %w", err) | ||
} | ||
log.Debug("folicy " + policy + " downloaded from archivista") | ||
} | ||
|
||
} else { | ||
defer filePolicy.Close() | ||
decoder := json.NewDecoder(filePolicy) | ||
if err := decoder.Decode(&policyEnvelope); err != nil { | ||
return policyEnvelope, fmt.Errorf("could not unmarshal policy envelope: %w", err) | ||
} | ||
} | ||
|
||
return policyEnvelope, nil | ||
} |