| 1 |
// Copyright 2024 The Archivista Contributors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package rstuf |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"context" |
| 20 |
"crypto/sha256" |
| 21 |
"encoding/hex" |
| 22 |
"encoding/json" |
| 23 |
"fmt" |
| 24 |
"io" |
| 25 |
"net/http" |
| 26 |
"net/http/httputil" |
| 27 |
|
| 28 |
"github.com/in-toto/archivista/pkg/config" |
| 29 |
"github.com/sirupsen/logrus" |
| 30 |
) |
| 31 |
|
| 32 |
type RSTUF struct { |
| 33 |
Host string |
| 34 |
} |
| 35 |
|
| 36 |
type Publisher interface { |
| 37 |
Publish(ctx context.Context, gitoid string, payload []byte) error |
| 38 |
} |
| 39 |
|
| 40 |
func (r *RSTUF) parseRSTUFPayload(gitoid string, payload []byte) ([]byte, error) { |
| 41 |
objHash := sha256.Sum256(payload) |
| 42 |
// custom := make(map[string]any) |
| 43 |
// custom["gitoid"] = gitoid |
| 44 |
artifacts := []Artifact{ |
| 45 |
{ |
| 46 |
Path: gitoid, |
| 47 |
Info: ArtifactInfo{ |
| 48 |
Length: len(payload), |
| 49 |
Hashes: Hashes{ |
| 50 |
Sha256: hex.EncodeToString(objHash[:]), |
| 51 |
}, |
| 52 |
// Custom: custom, |
| 53 |
}, |
| 54 |
}, |
| 55 |
} |
| 56 |
|
| 57 |
artifactPayload := ArtifactPayload{ |
| 58 |
Artifacts: artifacts, |
| 59 |
AddTaskIDToCustom: false, |
| 60 |
PublishTargets: true, |
| 61 |
} |
| 62 |
|
| 63 |
payloadBytes, err := json.Marshal(artifactPayload) |
| 64 |
if err != nil { |
| 65 |
return nil, fmt.Errorf("error marshaling payload: %v", err) |
| 66 |
} |
| 67 |
return payloadBytes, nil |
| 68 |
} |
| 69 |
|
| 70 |
func (r *RSTUF) Publish(ctx context.Context, gitoid string, payload []byte) error { |
| 71 |
// this publisher allows integration with the RSTUF project to store |
| 72 |
// the attestation and policy in the TUF metadata. |
| 73 |
// this TUF metadata can be used to build truste when distributing the |
| 74 |
// attestations and policies. |
| 75 |
// Convert payload to JSON |
| 76 |
url := r.Host + "/api/v1/artifacts" |
| 77 |
|
| 78 |
payloadBytes, err := r.parseRSTUFPayload(gitoid, payload) |
| 79 |
if err != nil { |
| 80 |
return fmt.Errorf("error parsing payload: %v", err) |
| 81 |
} |
| 82 |
|
| 83 |
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes)) |
| 84 |
if err != nil { |
| 85 |
return fmt.Errorf("error creating request: %v", err) |
| 86 |
} |
| 87 |
|
| 88 |
req.Header.Set("Content-Type", "application/json") |
| 89 |
// Add any additional headers or authentication if needed |
| 90 |
|
| 91 |
client := &http.Client{} |
| 92 |
resp, err := client.Do(req) |
| 93 |
if err != nil { |
| 94 |
logrus.Errorf("error making request: %v", err) |
| 95 |
} |
| 96 |
defer resp.Body.Close() |
| 97 |
|
| 98 |
if resp.StatusCode != http.StatusAccepted { |
| 99 |
logb, _ := httputil.DumpResponse(resp, true) |
| 100 |
logrus.Errorf("error body from RSTUF: %v", string(logb)) |
| 101 |
return fmt.Errorf("error response from RSTUF: %v", err) |
| 102 |
} |
| 103 |
|
| 104 |
// Handle the response as needed |
| 105 |
body, err := io.ReadAll(resp.Body) |
| 106 |
if err != nil { |
| 107 |
logrus.Errorf("error reading response body: %v", err) |
| 108 |
} |
| 109 |
|
| 110 |
response := Response{} |
| 111 |
err = json.Unmarshal(body, &response) |
| 112 |
if err != nil { |
| 113 |
logrus.Errorf("error unmarshaling response: %v", err) |
| 114 |
} |
| 115 |
logrus.Debugf("RSTUF task id: %v", response.Data.TaskId) |
| 116 |
// TODO: monitor RSTUF task id for completion |
| 117 |
return nil |
| 118 |
} |
| 119 |
|
| 120 |
func NewPublisher(config *config.Config) Publisher { |
| 121 |
return &RSTUF{ |
| 122 |
Host: config.PublisherRstufHost, |
| 123 |
} |
| 124 |
} |