-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(referrers): API endpoint (#424)
Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
- Loading branch information
Showing
18 changed files
with
1,800 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// Copyright 2023 The Chainloop 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 cmd | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/chainloop-dev/chainloop/app/cli/internal/action" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newReferrerDiscoverCmd() *cobra.Command { | ||
var digest string | ||
|
||
cmd := &cobra.Command{ | ||
Use: "discover", | ||
Short: "(Preview) inspect pieces of evidence or artifacts stored through Chainloop", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
res, err := action.NewReferrerDiscover(actionOpts).Run(context.Background(), digest) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// NOTE: this is a preview/beta command, for now we only return JSON format | ||
return encodeJSON(res) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVarP(&digest, "digest", "d", "", "hash of the attestation, piece of evidence or artifact, i.e sha256:deadbeef") | ||
err := cmd.MarkFlagRequired("digest") | ||
cobra.CheckErr(err) | ||
|
||
return cmd | ||
} |
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,69 @@ | ||
// | ||
// Copyright 2023 The Chainloop 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 action | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1" | ||
) | ||
|
||
type ReferrerDiscover struct { | ||
cfg *ActionsOpts | ||
} | ||
|
||
type ReferrerItem struct { | ||
Digest string `json:"digest"` | ||
Kind string `json:"kind"` | ||
Downloadable bool `json:"downloadable"` | ||
CreatedAt *time.Time `json:"createdAt"` | ||
References []*ReferrerItem `json:"references"` | ||
} | ||
|
||
func NewReferrerDiscover(cfg *ActionsOpts) *ReferrerDiscover { | ||
return &ReferrerDiscover{cfg} | ||
} | ||
|
||
func (action *ReferrerDiscover) Run(ctx context.Context, digest string) (*ReferrerItem, error) { | ||
client := pb.NewReferrerServiceClient(action.cfg.CPConnection) | ||
resp, err := client.Discover(ctx, &pb.ReferrerServiceDiscoverRequest{Digest: digest}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return pbReferrerItemToAction(resp.Result), nil | ||
} | ||
|
||
func pbReferrerItemToAction(in *pb.ReferrerItem) *ReferrerItem { | ||
if in == nil { | ||
return nil | ||
} | ||
|
||
out := &ReferrerItem{ | ||
Digest: in.GetDigest(), | ||
Downloadable: in.GetDownloadable(), | ||
Kind: in.GetKind(), | ||
CreatedAt: toTimePtr(in.GetCreatedAt().AsTime()), | ||
References: make([]*ReferrerItem, 0, len(in.GetReferences())), | ||
} | ||
|
||
for _, r := range in.GetReferences() { | ||
out.References = append(out.References, pbReferrerItemToAction(r)) | ||
} | ||
|
||
return out | ||
} |
Oops, something went wrong.