-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UNTESTED: Replace sigstore/rekor/pkg/client with a manually-created c…
…lient This removes 4.728 MB from a macOS Skopeo binary. Signed-off-by: Miloslav Trmač <[email protected]>
- Loading branch information
Showing
9 changed files
with
421 additions
and
311 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,93 @@ | ||
package internal | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
const rekorHashedrekordKind = "hashedrekord" | ||
|
||
type RekorHashedrekord struct { | ||
APIVersion *string `json:"apiVersion"` | ||
Spec json.RawMessage `json:"spec"` | ||
} | ||
|
||
func (m *RekorHashedrekord) Kind() string { | ||
return rekorHashedrekordKind | ||
} | ||
|
||
func (m *RekorHashedrekord) SetKind(val string) { | ||
} | ||
|
||
func (m *RekorHashedrekord) UnmarshalJSON(raw []byte) error { | ||
var base struct { | ||
Kind string `json:"kind"` | ||
} | ||
dec := json.NewDecoder(bytes.NewReader(raw)) | ||
dec.UseNumber() | ||
if err := dec.Decode(&base); err != nil { | ||
return err | ||
} | ||
|
||
switch base.Kind { | ||
case rekorHashedrekordKind: | ||
var data struct { // We can’t use RekorHashedRekord directly, because that would be an infinite recursion. | ||
APIVersion *string `json:"apiVersion"` | ||
Spec json.RawMessage `json:"spec"` | ||
} | ||
dec = json.NewDecoder(bytes.NewReader(raw)) | ||
dec.UseNumber() | ||
if err := dec.Decode(&data); err != nil { | ||
return err | ||
} | ||
res := RekorHashedrekord{ | ||
APIVersion: data.APIVersion, | ||
Spec: data.Spec, | ||
} | ||
*m = res | ||
return nil | ||
|
||
default: | ||
return fmt.Errorf("invalid kind value: %q", base.Kind) | ||
} | ||
} | ||
|
||
func (m RekorHashedrekord) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(struct { | ||
Kind string `json:"kind"` | ||
APIVersion *string `json:"apiVersion"` | ||
Spec json.RawMessage `json:"spec"` | ||
}{ | ||
Kind: m.Kind(), | ||
APIVersion: m.APIVersion, | ||
Spec: m.Spec, | ||
}) | ||
} | ||
|
||
type RekorHashedrekordV001Schema struct { | ||
Data *RekorHashedrekordV001SchemaData `json:"data"` | ||
Signature *RekorHashedrekordV001SchemaSignature `json:"signature"` | ||
} | ||
|
||
type RekorHashedrekordV001SchemaData struct { | ||
Hash *RekorHashedrekordV001SchemaDataHash `json:"hash,omitempty"` | ||
} | ||
|
||
type RekorHashedrekordV001SchemaDataHash struct { | ||
Algorithm *string `json:"algorithm"` | ||
Value *string `json:"value"` | ||
} | ||
|
||
const ( | ||
RekorHashedrekordV001SchemaDataHashAlgorithmSha256 string = "sha256" | ||
) | ||
|
||
type RekorHashedrekordV001SchemaSignature struct { | ||
Content []byte `json:"content,omitempty"` | ||
PublicKey *RekorHashedrekordV001SchemaSignaturePublicKey `json:"publicKey,omitempty"` | ||
} | ||
|
||
type RekorHashedrekordV001SchemaSignaturePublicKey struct { | ||
Content []byte `json:"content,omitempty"` | ||
} |
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,79 @@ | ||
package rekor | ||
|
||
// The following code is the essence of the relevant code paths from github.com/go-openapi/runtime, | ||
// heavily modified since. | ||
|
||
// Copyright 2015 go-swagger maintainers | ||
// | ||
// 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. | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"path" | ||
) | ||
|
||
// makeRequest makes a http request to the requested requestPath, and returns the received response. | ||
func (r *rekorClient) makeRequest(ctx context.Context, method, requestPath string, bodyContent any) (*http.Response, error) { | ||
ctx, cancel := context.WithCancel(ctx) | ||
defer cancel() | ||
|
||
var body io.Reader | ||
headers := http.Header{} | ||
|
||
headers.Set("Accept", "application/json") | ||
if bodyContent != nil { | ||
buf := bytes.NewBuffer(nil) | ||
body = buf | ||
headers.Set("Content-Type", "application/json") | ||
enc := json.NewEncoder(buf) | ||
enc.SetEscapeHTML(false) | ||
if err := enc.Encode(bodyContent); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
req, err := http.NewRequestWithContext(ctx, method, path.Join(r.basePath, requestPath), body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Only Scheme and Host are used from rekorURL. | ||
// Really this should probabbly use r.rekorURL.JoinPath(requestPath) (which, notably, correctly deals with path escaping), | ||
// and pass that to NewRequestWithContext, but this use of path.Join is consistent with go-openapi/runtime v0.24.1 . | ||
req.URL.Scheme = r.rekorURL.Scheme | ||
req.URL.Host = r.rekorURL.Host | ||
req.Header = headers | ||
|
||
res, err := r.httpClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Note that we don’t care to even read the Content-Type: header; we blindly assume the format is the requested JSON. | ||
return res, nil | ||
} | ||
|
||
// decodeHTTPResponseBodyAsJSON decodes the body of a HTTP response in a manner compatible with github.com/go-openapi/runtime. | ||
func decodeHTTPResponseBodyAsJSON(res *http.Response, data any) error { | ||
dec := json.NewDecoder(res.Body) | ||
dec.UseNumber() | ||
err := dec.Decode(data) | ||
if err == io.EOF { | ||
// This seems unwanted at a first glance; go-swagger added it in https://github.com/go-swagger/go-swagger/issues/192 , it’s unclear | ||
// whether it’s correct or still necessary. | ||
err = nil | ||
} | ||
return err | ||
} |
Oops, something went wrong.