configapi: simplify pkg structure

Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
This commit is contained in:
Paul Meyer 2023-08-10 09:45:46 +02:00
parent f604a8dfd2
commit 5bfaae2304
7 changed files with 55 additions and 80 deletions

View File

@ -1,11 +1,21 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("//bazel/go:go_test.bzl", "go_test")
go_library(
name = "configapi_lib",
srcs = ["main.go"],
srcs = [
"delete.go",
"root.go",
],
importpath = "github.com/edgelesssys/constellation/v2/hack/configapi",
visibility = ["//visibility:private"],
deps = ["//hack/configapi/cmd"],
deps = [
"//internal/api/attestationconfigapi",
"//internal/logger",
"//internal/staticupload",
"@com_github_spf13_cobra//:cobra",
"@org_uber_go_zap//:zap",
],
)
go_binary(
@ -13,3 +23,17 @@ go_binary(
embed = [":configapi_lib"],
visibility = ["//visibility:public"],
)
go_test(
name = "configapi_test",
srcs = [
"delete_test.go",
"root_test.go",
],
embed = [":configapi_lib"],
deps = [
"//internal/api/attestationconfigapi",
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require",
],
)

View File

@ -1,33 +0,0 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("//bazel/go:go_test.bzl", "go_test")
go_library(
name = "cmd",
srcs = [
"delete.go",
"root.go",
],
importpath = "github.com/edgelesssys/constellation/v2/hack/configapi/cmd",
visibility = ["//visibility:public"],
deps = [
"//internal/api/attestationconfigapi",
"//internal/logger",
"//internal/staticupload",
"@com_github_spf13_cobra//:cobra",
"@org_uber_go_zap//:zap",
],
)
go_test(
name = "cmd_test",
srcs = [
"delete_test.go",
"root_test.go",
],
embed = [":cmd"],
deps = [
"//internal/api/attestationconfigapi",
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require",
],
)

View File

@ -3,7 +3,7 @@ Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package cmd
package main
import (
"context"
@ -50,13 +50,13 @@ func runDelete(cmd *cobra.Command, _ []string) error {
Bucket: awsBucket,
Region: awsRegion,
}
client, close, err := attestationconfigapi.NewClient(cmd.Context(), cfg, []byte(cosignPwd), []byte(privateKey), false, log)
client, stop, err := attestationconfigapi.NewClient(cmd.Context(), cfg, []byte(cosignPwd), []byte(privateKey), false, log)
if err != nil {
return fmt.Errorf("create attestation client: %w", err)
}
defer func() {
if err := close(cmd.Context()); err != nil {
cmd.Printf("close client: %s\n", err.Error())
if err := stop(cmd.Context()); err != nil {
cmd.Printf("stopping client: %s\n", err.Error())
}
}()
deleteCmd := deleteCmd{

View File

@ -3,7 +3,7 @@ Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package cmd
package main
import (
"context"

View File

@ -1,20 +0,0 @@
/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package main
import (
"os"
"github.com/edgelesssys/constellation/v2/hack/configapi/cmd"
)
func main() {
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
os.Exit(0)
}

View File

@ -4,7 +4,7 @@ Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package cmd
package main
import (
"encoding/json"
@ -34,9 +34,11 @@ var (
privateKey string
)
// Execute executes the root command.
func Execute() error {
return newRootCmd().Execute()
func main() {
if err := newRootCmd().Execute(); err != nil {
os.Exit(1)
}
os.Exit(0)
}
// newRootCmd creates the root command.

View File

@ -4,7 +4,7 @@ Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package cmd
package main
import (
"testing"
@ -13,14 +13,16 @@ import (
"github.com/stretchr/testify/assert"
)
var testCfg = attestationconfigapi.AzureSEVSNPVersion{
func TestIsInputNewerThanLatestAPI(t *testing.T) {
newTestCfg := func() attestationconfigapi.AzureSEVSNPVersion {
return attestationconfigapi.AzureSEVSNPVersion{
Microcode: 93,
TEE: 0,
SNP: 6,
Bootloader: 2,
}
}
func TestIsInputNewerThanLatestAPI(t *testing.T) {
testCases := map[string]struct {
latest attestationconfigapi.AzureSEVSNPVersion
input attestationconfigapi.AzureSEVSNPVersion
@ -31,8 +33,8 @@ func TestIsInputNewerThanLatestAPI(t *testing.T) {
input: func(c attestationconfigapi.AzureSEVSNPVersion) attestationconfigapi.AzureSEVSNPVersion {
c.Microcode--
return c
}(testCfg),
latest: testCfg,
}(newTestCfg()),
latest: newTestCfg(),
expect: false,
errMsg: "input Microcode version: 92 is older than latest API version: 93",
},
@ -41,8 +43,8 @@ func TestIsInputNewerThanLatestAPI(t *testing.T) {
c.Microcode++
c.Bootloader--
return c
}(testCfg),
latest: testCfg,
}(newTestCfg()),
latest: newTestCfg(),
expect: false,
errMsg: "input Bootloader version: 1 is older than latest API version: 2",
},
@ -50,13 +52,13 @@ func TestIsInputNewerThanLatestAPI(t *testing.T) {
input: func(c attestationconfigapi.AzureSEVSNPVersion) attestationconfigapi.AzureSEVSNPVersion {
c.TEE++
return c
}(testCfg),
latest: testCfg,
}(newTestCfg()),
latest: newTestCfg(),
expect: true,
},
"input is equal to latest": {
input: testCfg,
latest: testCfg,
input: newTestCfg(),
latest: newTestCfg(),
expect: false,
},
}