-
Notifications
You must be signed in to change notification settings - Fork 380
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
412 additions
and
256 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,76 @@ | ||
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" | ||
) | ||
|
||
// makeRequest makes a http request to the requested path, and returns the received response. | ||
func (r *rekorClient) makeRequest(ctx context.Context, method, path 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, body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Only Scheme and Host are used from rekorURL; Path comes from the parameter. | ||
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.