Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move must code to new package. #45

Merged
merged 2 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/veil-verify/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/Amnesic-Systems/veil/internal/httpx"
"github.com/Amnesic-Systems/veil/internal/nonce"
"github.com/Amnesic-Systems/veil/internal/service"
"github.com/Amnesic-Systems/veil/internal/util"
"github.com/Amnesic-Systems/veil/internal/util/must"
)

var (
Expand Down Expand Up @@ -165,8 +165,8 @@ func toPCR(jsonMsmts []byte) (_ enclave.PCR, err error) {
}

return enclave.PCR{
0: util.Must(hex.DecodeString(m.Measurements.PCR0)),
1: util.Must(hex.DecodeString(m.Measurements.PCR1)),
2: util.Must(hex.DecodeString(m.Measurements.PCR2)),
0: must.Get(hex.DecodeString(m.Measurements.PCR0)),
1: must.Get(hex.DecodeString(m.Measurements.PCR1)),
2: must.Get(hex.DecodeString(m.Measurements.PCR2)),
}, nil
}
10 changes: 5 additions & 5 deletions cmd/veil/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/Amnesic-Systems/veil/internal/service"
"github.com/Amnesic-Systems/veil/internal/service/attestation"
"github.com/Amnesic-Systems/veil/internal/testutil"
"github.com/Amnesic-Systems/veil/internal/util"
"github.com/Amnesic-Systems/veil/internal/util/must"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -166,7 +166,7 @@ func TestPages(t *testing.T) {
{
name: "config with nonce",
url: extSrv(service.PathConfig + "?nonce=" +
util.Must(nonce.New()).URLEncode(),
must.Get(nonce.New()).URLEncode(),
),
wantBody: `"Debug":false`,
},
Expand Down Expand Up @@ -195,7 +195,7 @@ func TestEnclaveCodeURI(t *testing.T) {
require.Equal(t, http.StatusOK, resp.StatusCode)
defer resp.Body.Close()

body := util.Must(io.ReadAll(resp.Body))
body := must.Get(io.ReadAll(resp.Body))
require.Contains(t, string(body), codeURI)
}

Expand Down Expand Up @@ -268,7 +268,7 @@ func TestAttestation(t *testing.T) {
{
name: "valid attestation request",
url: extSrv(service.PathAttestation),
nonce: util.Must(nonce.New()),
nonce: must.Get(nonce.New()),
wantCode: http.StatusOK,
},
}
Expand Down Expand Up @@ -451,7 +451,7 @@ func TestRunApp(t *testing.T) {
"https://localhost:%d"+service.PathConfig+"?nonce=%s",
fd.Name(),
defaultExtPort,
util.Must(nonce.New()).URLEncode(),
must.Get(nonce.New()).URLEncode(),
),
},
}
Expand Down
12 changes: 6 additions & 6 deletions internal/enclave/nitro/attester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/Amnesic-Systems/veil/internal/enclave"
"github.com/Amnesic-Systems/veil/internal/nonce"
"github.com/Amnesic-Systems/veil/internal/util"
"github.com/Amnesic-Systems/veil/internal/util/must"
"github.com/stretchr/testify/require"
)

Expand All @@ -31,7 +31,7 @@ func TestNitroAttest(t *testing.T) {
{
name: "aux info with nonce",
aux: &enclave.AuxInfo{
Nonce: util.Must(nonce.New()).ToSlice(),
Nonce: must.Get(nonce.New()).ToSlice(),
},
},
}
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestNitroVerify(t *testing.T) {
require.NoError(t, err)
return doc
}
testNonce := util.Must(nonce.New())
testNonce := must.Get(nonce.New())

cases := []struct {
name string
Expand All @@ -86,13 +86,13 @@ func TestNitroVerify(t *testing.T) {
},
{
name: "nonce mismatch",
doc: getDoc(t, util.Must(nonce.New())),
nonce: util.Must(nonce.New()),
doc: getDoc(t, must.Get(nonce.New())),
nonce: must.Get(nonce.New()),
wantErr: true,
},
{
name: "no nonce",
doc: getDoc(t, util.Must(nonce.New())),
doc: getDoc(t, must.Get(nonce.New())),
},
{
name: "valid document and nonce",
Expand Down
12 changes: 6 additions & 6 deletions internal/httpx/httpx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/Amnesic-Systems/veil/internal/errs"
"github.com/Amnesic-Systems/veil/internal/nonce"
"github.com/Amnesic-Systems/veil/internal/util"
"github.com/Amnesic-Systems/veil/internal/util/must"
)

func TestWaitForSvc(t *testing.T) {
Expand Down Expand Up @@ -68,35 +68,35 @@ func TestExtractNonce(t *testing.T) {
name: "invalid form",
req: &http.Request{
// Semicolons aren't allowed in the query.
URL: util.Must(url.Parse("https://example.com/endpoint?;")),
URL: must.Get(url.Parse("https://example.com/endpoint?;")),
},
wantErr: errBadForm,
},
{
name: "no nonce",
req: &http.Request{
URL: util.Must(url.Parse("https://example.com/endpoint?foo=bar")),
URL: must.Get(url.Parse("https://example.com/endpoint?foo=bar")),
},
wantErr: errNoNonce,
},
{
name: "bad nonce format",
req: &http.Request{
URL: util.Must(url.Parse("https://example.com/endpoint?nonce=%21")),
URL: must.Get(url.Parse("https://example.com/endpoint?nonce=%21")),
},
wantErr: errBadNonceFormat,
},
{
name: "nonce too short",
req: &http.Request{
URL: util.Must(url.Parse("https://example.com/endpoint?nonce=AAAAAAAAAAAAAA%3D%3D")),
URL: must.Get(url.Parse("https://example.com/endpoint?nonce=AAAAAAAAAAAAAA%3D%3D")),
},
wantErr: errs.InvalidLength,
},
{
name: "valid nonce",
req: &http.Request{
URL: util.Must(url.Parse("https://example.com/endpoint?nonce=AAAAAAAAAAAAAAAAAAAAAAAAAAA%3D")),
URL: must.Get(url.Parse("https://example.com/endpoint?nonce=AAAAAAAAAAAAAAAAAAAAAAAAAAA%3D")),
},
wantNonce: &nonce.Nonce{},
},
Expand Down
4 changes: 2 additions & 2 deletions internal/service/attestation/aux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"github.com/Amnesic-Systems/veil/internal/enclave"
"github.com/Amnesic-Systems/veil/internal/errs"
"github.com/Amnesic-Systems/veil/internal/nonce"
"github.com/Amnesic-Systems/veil/internal/util"
"github.com/Amnesic-Systems/veil/internal/util/must"
"github.com/stretchr/testify/require"
)

func TestGetters(t *testing.T) {
n := util.Must(nonce.New())
n := must.Get(nonce.New())
s := addr.Of(sha256.Sum256([]byte("foo")))
h1 := &Hashes{TlsKeyHash: addr.Of(sha256.Sum256([]byte("foo")))}
h2 := &Hashes{
Expand Down
4 changes: 2 additions & 2 deletions internal/service/attestation/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/Amnesic-Systems/veil/internal/enclave/nitro"
"github.com/Amnesic-Systems/veil/internal/enclave/noop"
"github.com/Amnesic-Systems/veil/internal/nonce"
"github.com/Amnesic-Systems/veil/internal/util"
"github.com/Amnesic-Systems/veil/internal/util/must"
"github.com/stretchr/testify/require"
)

Expand All @@ -18,7 +18,7 @@ func TestBuilder(t *testing.T) {
if nitro.IsEnclave() {
attester = nitro.NewAttester()
}
nonce1, nonce2 := util.Must(nonce.New()), util.Must(nonce.New())
nonce1, nonce2 := must.Get(nonce.New()), must.Get(nonce.New())
sha1, sha2 := sha256.Sum256([]byte("foo")), sha256.Sum256([]byte("bar"))
hashes1 := &Hashes{TlsKeyHash: addr.Of(sha256.Sum256([]byte("foo")))}
hashes2 := &Hashes{TlsKeyHash: addr.Of(sha256.Sum256([]byte("bar")))}
Expand Down
4 changes: 2 additions & 2 deletions internal/service/handle/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/Amnesic-Systems/veil/internal/httperr"
"github.com/Amnesic-Systems/veil/internal/nonce"
"github.com/Amnesic-Systems/veil/internal/service/attestation"
"github.com/Amnesic-Systems/veil/internal/util"
"github.com/Amnesic-Systems/veil/internal/util/must"
)

func TestEncodeAndAttest(t *testing.T) {
Expand Down Expand Up @@ -44,7 +44,7 @@ func TestEncodeAndAttest(t *testing.T) {
},
{
name: "everything valid",
nonce: util.Must(nonce.New()),
nonce: must.Get(nonce.New()),
wantStatus: http.StatusOK,
body: httperr.New("random error"),
wantBody: `{"error":"random error"}` + "\n",
Expand Down
4 changes: 2 additions & 2 deletions internal/service/handle/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/Amnesic-Systems/veil/internal/httperr"
"github.com/Amnesic-Systems/veil/internal/httpx"
"github.com/Amnesic-Systems/veil/internal/service/attestation"
"github.com/Amnesic-Systems/veil/internal/util"
"github.com/Amnesic-Systems/veil/internal/util/must"
)

// Index informs the visitor that this host runs inside an enclave. This is
Expand Down Expand Up @@ -62,7 +62,7 @@ func Hashes(hashes *attestation.Hashes) http.HandlerFunc {
func AppHash(
setAppHash func(*[sha256.Size]byte),
) http.HandlerFunc {
b := util.Must(json.Marshal(&attestation.Hashes{
b := must.Get(json.Marshal(&attestation.Hashes{
TlsKeyHash: addr.Of(sha256.Sum256([]byte("foo"))),
AppKeyHash: addr.Of(sha256.Sum256([]byte("bar"))),
}))
Expand Down
4 changes: 2 additions & 2 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/Amnesic-Systems/veil/internal/service/attestation"
"github.com/Amnesic-Systems/veil/internal/system"
"github.com/Amnesic-Systems/veil/internal/tunnel"
"github.com/Amnesic-Systems/veil/internal/util"
"github.com/Amnesic-Systems/veil/internal/util/must"
"github.com/go-chi/chi/v5"
)

Expand Down Expand Up @@ -58,7 +58,7 @@ func Run(
extSrv := newExtSrv(config, builder)
extSrv.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{
util.Must(tls.X509KeyPair(cert, key)),
must.Get(tls.X509KeyPair(cert, key)),
},
}

Expand Down
8 changes: 0 additions & 8 deletions internal/util/common.go

This file was deleted.

10 changes: 10 additions & 0 deletions internal/util/must/must.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Package must provides functions to perform an action or panic.
package must

// Get returns the value of v if err is nil and panics otherwise.
func Get[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package must

import (
"errors"
Expand All @@ -10,8 +10,8 @@ import (
func TestMust(t *testing.T) {
t.Parallel()

require.Equal(t, 1, Must(1, nil))
require.Equal(t, 1, Get(1, nil))
require.Panics(t, func() {
_ = Must("foo", errors.New("an error"))
_ = Get("foo", errors.New("an error"))
})
}
Loading