| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package signersscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/ |
| 18 | |
| 19 | // Some package types can't be signed as a stream as-is, so we transform them |
| 20 | // into something else (a tarball) and upload that to the server. The server |
| 21 | // signs the tar, returns the signature blob, and the client inserts the blob |
| 22 | // into the original file. This mechanism can also be used for cases that don't |
| 23 | // need a transform on the upload but do need special processing on the result |
| 24 | // side. A default implementation that handles patching, copying, and |
| 25 | // overwriting is provided. |
| 26 | |
| 27 | import ( |
| 28 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 29 | "ioscip-go gomod github.com/golang/go/src go1.18 io/" |
| 30 | "io/ioutilscip-go gomod github.com/golang/go/src go1.18 `io/ioutil`/" |
| 31 | "osscip-go gomod github.com/golang/go/src go1.18 os/" |
| 32 | |
| 33 | "github.com/sassoftware/relic/v7/lib/atomicfilescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/atomicfile`/" |
| 34 | "github.com/sassoftware/relic/v7/lib/binpatchscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/" |
| 35 | ) |
| 36 | |
| 37 | type Transformerscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/Transformer# interface { |
| 38 | // Return a stream that will be uploaded to a remote server. This may be |
| 39 | // called multiple times in case of failover. |
| 40 | GetReaderscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/Transformer#GetReader.() (stream ioscip-go gomod github.com/golang/go/src go1.18 io/.Readerscip-go gomod github.com/golang/go/src go1.18 io/Reader#, err error) |
| 41 | // Apply a HTTP response to the named destination file |
| 42 | Applyscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/Transformer#Apply.(dest, mimetype string, result ioscip-go gomod github.com/golang/go/src go1.18 io/.Readerscip-go gomod github.com/golang/go/src go1.18 io/Reader#) error |
| 43 | } |
| 44 | |
| 45 | // Return the transform for the given module if it has one, otherwise return |
| 46 | // the default transform. |
| 47 | func (s *Signerscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/Signer#) GetTransformscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/Signer#GetTransform().(f *osscip-go gomod github.com/golang/go/src go1.18 os/.Filescip-go gomod github.com/golang/go/src go1.18 os/File#, opts SignOptsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/SignOpts#) (Transformerscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/Transformer#, error) { |
| 48 | if s != nil && s.Transformscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/Signer#Transform. != nil { |
| 49 | return s.Transformscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/Signer#Transform.(f, opts) |
| 50 | } |
| 51 | return fileProducerscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/fileProducer#{f}, nil |
| 52 | } |
| 53 | |
| 54 | func DefaultTransformscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/DefaultTransform().(f *osscip-go gomod github.com/golang/go/src go1.18 os/.Filescip-go gomod github.com/golang/go/src go1.18 os/File#) Transformerscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/Transformer# { |
| 55 | return fileProducerscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/fileProducer#{f} |
| 56 | } |
| 57 | |
| 58 | // Dummy implementation that sends the original file as a request, and |
| 59 | // either applies a binary patch or overwrites the whole file depending on the |
| 60 | // MIME type. |
| 61 | |
| 62 | type fileProducerscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/fileProducer# struct { |
| 63 | fscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/fileProducer#f. *osscip-go gomod github.com/golang/go/src go1.18 os/.Filescip-go gomod github.com/golang/go/src go1.18 os/File# |
| 64 | } |
| 65 | |
| 66 | func (p fileProducerscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/fileProducer#) GetReaderscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/fileProducer#GetReader().() (ioscip-go gomod github.com/golang/go/src go1.18 io/.Readerscip-go gomod github.com/golang/go/src go1.18 io/Reader#, error) { |
| 67 | if _, err := p.fscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/fileProducer#f..Seekscip-go gomod github.com/golang/go/src go1.18 os/File#Seek().(0, ioscip-go gomod github.com/golang/go/src go1.18 io/.SeekStartscip-go gomod github.com/golang/go/src go1.18 io/SeekStart.); err != nil { |
| 68 | return nil, fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("seeking input file: %w", err) |
| 69 | } |
| 70 | return p.fscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/fileProducer#f., nil |
| 71 | } |
| 72 | |
| 73 | // If the response is a binpatch, apply it. Otherwise overwrite the destination |
| 74 | // file with the response |
| 75 | func (p fileProducerscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/fileProducer#) Applyscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/fileProducer#Apply().(dest, mimetype string, result ioscip-go gomod github.com/golang/go/src go1.18 io/.Readerscip-go gomod github.com/golang/go/src go1.18 io/Reader#) error { |
| 76 | if mimetype == binpatchscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/.MimeTypescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/MimeType. { |
| 77 | return ApplyBinPatchscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/ApplyBinPatch().(p.fscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/fileProducer#f., dest, result) |
| 78 | } |
| 79 | f, err := atomicfilescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/atomicfile`/.WriteAnyscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/atomicfile`/WriteAny().(dest) |
| 80 | if err != nil { |
| 81 | return err |
| 82 | } |
| 83 | if _, err := ioscip-go gomod github.com/golang/go/src go1.18 io/.Copyscip-go gomod github.com/golang/go/src go1.18 io/Copy().(f, result); err != nil { |
| 84 | return err |
| 85 | } |
| 86 | p.fscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/fileProducer#f..Closescip-go gomod github.com/golang/go/src go1.18 os/File#Close().() |
| 87 | return f.Commitscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/atomicfile`/AtomicFile#Commit.() |
| 88 | } |
| 89 | |
| 90 | func ApplyBinPatchscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/signers`/ApplyBinPatch().(src *osscip-go gomod github.com/golang/go/src go1.18 os/.Filescip-go gomod github.com/golang/go/src go1.18 os/File#, dest string, result ioscip-go gomod github.com/golang/go/src go1.18 io/.Readerscip-go gomod github.com/golang/go/src go1.18 io/Reader#) error { |
| 91 | blob, err := ioutilscip-go gomod github.com/golang/go/src go1.18 `io/ioutil`/.ReadAllscip-go gomod github.com/golang/go/src go1.18 `io/ioutil`/ReadAll().(result) |
| 92 | if err != nil { |
| 93 | return err |
| 94 | } |
| 95 | patch, err := binpatchscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/.Loadscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/Load().(blob) |
| 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | return patch.Applyscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/binpatch`/PatchSet#Apply().(src, dest) |
| 100 | } |