Skip to content

Commit

Permalink
Split pkg/server from pkg/api (#616)
Browse files Browse the repository at this point in the history
API clients using pkg/api.NewClient , like github.com/sigstore/cosign,
currently end up depending on all the implementation details of the servers,
unnecessarily increasing their binary size and supply-chain attack surface.

Instead, move the server code to a new pkg/server (assuming there are
no external users of that functionality), and leave pkg/api as a package only
providing a client (as it is used in practice, and documented in various
places in this repo).

The move includes pkg/api/version.go, because it is only useful
with a Go build that specifies the various values at build time,
which is unlikely to be the case for any third-party importer
of that package.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
  • Loading branch information
mtrmac authored May 26, 2022
1 parent 85372aa commit 1c17c94
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ issues:
- errcheck
- gosec
# the following section is due to the legacy API being deprecated
- path: pkg/api/legacy_server.go
- path: pkg/server/legacy_server.go
linters:
- staticcheck
text: SA1019
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ ifeq ($(DIFF), 1)
GIT_TREESTATE = "dirty"
endif

FULCIO_PKG=github.com/sigstore/fulcio/pkg/api
LDFLAGS=-X $(FULCIO_PKG).gitVersion=$(GIT_VERSION) -X $(FULCIO_PKG).gitCommit=$(GIT_HASH) -X $(FULCIO_PKG).gitTreeState=$(GIT_TREESTATE) -X $(FULCIO_PKG).buildDate=$(BUILD_DATE)
FULCIO_VERSION_PKG=github.com/sigstore/fulcio/pkg/server
LDFLAGS=-X $(FULCIO_VERSION_PKG).gitVersion=$(GIT_VERSION) -X $(FULCIO_VERSION_PKG).gitCommit=$(GIT_HASH) -X $(FULCIO_VERSION_PKG).gitTreeState=$(GIT_TREESTATE) -X $(FULCIO_VERSION_PKG).buildDate=$(BUILD_DATE)

KO_PREFIX ?= gcr.io/projectsigstore
export KO_DOCKER_REPO=$(KO_PREFIX)
Expand Down
8 changes: 4 additions & 4 deletions cmd/app/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import (
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/sigstore/fulcio/pkg/api"
"github.com/sigstore/fulcio/pkg/ca"
"github.com/sigstore/fulcio/pkg/config"
gw "github.com/sigstore/fulcio/pkg/generated/protobuf"
gw_legacy "github.com/sigstore/fulcio/pkg/generated/protobuf/legacy"
"github.com/sigstore/fulcio/pkg/log"
"github.com/sigstore/fulcio/pkg/server"
"github.com/spf13/viper"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -75,7 +75,7 @@ func createGRPCServer(cfg *config.FulcioConfig, ctClient *ctclient.LogClient, ba
)),
grpc.MaxRecvMsgSize(int(maxMsgSize)))

grpcCAServer := api.NewGRPCCAServer(ctClient, baseca)
grpcCAServer := server.NewGRPCCAServer(ctClient, baseca)
// Register your gRPC service implementations.
gw.RegisterCAServer(myServer, grpcCAServer)

Expand All @@ -86,7 +86,7 @@ func createGRPCServer(cfg *config.FulcioConfig, ctClient *ctclient.LogClient, ba
func (g *grpcServer) setupPrometheus(reg *prometheus.Registry) {
grpcMetrics := grpc_prometheus.DefaultServerMetrics
grpcMetrics.EnableHandlingTimeHistogram()
reg.MustRegister(grpcMetrics, api.MetricLatency, api.RequestsCount)
reg.MustRegister(grpcMetrics, server.MetricLatency, server.RequestsCount)
grpc_prometheus.Register(g.Server)
}

Expand Down Expand Up @@ -137,7 +137,7 @@ func createLegacyGRPCServer(cfg *config.FulcioConfig, v2Server gw.CAServer) (*gr
)),
grpc.MaxRecvMsgSize(int(maxMsgSize)))

legacyGRPCCAServer := api.NewLegacyGRPCCAServer(v2Server)
legacyGRPCCAServer := server.NewLegacyGRPCCAServer(v2Server)

// Register your gRPC service implementations.
gw_legacy.RegisterCAServer(myServer, legacyGRPCCAServer)
Expand Down
18 changes: 9 additions & 9 deletions cmd/app/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import (

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sigstore/fulcio/pkg/api"
gw "github.com/sigstore/fulcio/pkg/generated/protobuf"
legacy_gw "github.com/sigstore/fulcio/pkg/generated/protobuf/legacy"
"github.com/sigstore/fulcio/pkg/log"
"github.com/sigstore/fulcio/pkg/server"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
Expand All @@ -43,7 +43,7 @@ type httpServer struct {

func extractOIDCTokenFromAuthHeader(ctx context.Context, req *http.Request) metadata.MD {
token := strings.Replace(req.Header.Get("Authorization"), "Bearer ", "", 1)
return metadata.Pairs(api.MetadataOIDCTokenKey, token)
return metadata.Pairs(server.MetadataOIDCTokenKey, token)
}

func createHTTPServer(ctx context.Context, serverEndpoint string, grpcServer, legacyGRPCServer *grpcServer) httpServer {
Expand All @@ -63,9 +63,9 @@ func createHTTPServer(ctx context.Context, serverEndpoint string, grpcServer, le
}

// Limit request size
handler := api.WithMaxBytes(mux, maxMsgSize)
handler = promhttp.InstrumentHandlerDuration(api.MetricLatency, handler)
handler = promhttp.InstrumentHandlerCounter(api.RequestsCount, handler)
handler := server.WithMaxBytes(mux, maxMsgSize)
handler = promhttp.InstrumentHandlerDuration(server.MetricLatency, handler)
handler = promhttp.InstrumentHandlerCounter(server.RequestsCount, handler)

api := http.Server{
Addr: serverEndpoint,
Expand Down Expand Up @@ -96,20 +96,20 @@ func setResponseCodeModifier(ctx context.Context, w http.ResponseWriter, _ proto
}

// set SCT if present ahead of modifying response code
if vals := md.HeaderMD.Get(api.SCTMetadataKey); len(vals) > 0 {
delete(md.HeaderMD, api.SCTMetadataKey)
if vals := md.HeaderMD.Get(server.SCTMetadataKey); len(vals) > 0 {
delete(md.HeaderMD, server.SCTMetadataKey)
delete(w.Header(), "Grpc-Metadata-sct")
w.Header().Set("SCT", vals[0])
}

// set http status code
if vals := md.HeaderMD.Get(api.HTTPResponseCodeMetadataKey); len(vals) > 0 {
if vals := md.HeaderMD.Get(server.HTTPResponseCodeMetadataKey); len(vals) > 0 {
code, err := strconv.Atoi(vals[0])
if err != nil {
return err
}
// delete the headers to not expose any grpc-metadata in http response
delete(md.HeaderMD, api.HTTPResponseCodeMetadataKey)
delete(md.HeaderMD, server.HTTPResponseCodeMetadataKey)
delete(w.Header(), "Grpc-Metadata-X-Http-Code")
w.WriteHeader(code)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/app/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package app
import (
"fmt"

"github.com/sigstore/fulcio/pkg/api"
"github.com/sigstore/fulcio/pkg/server"
"github.com/spf13/cobra"
)

Expand All @@ -43,7 +43,7 @@ func newVersionCmd() *cobra.Command {
}

func runVersion(opts *versionOptions) error {
v := api.VersionInfo()
v := server.VersionInfo()
res := v.String()

if opts.json {
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/error.go → pkg/server/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//

package api
package server

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/grpc_server.go → pkg/server/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//

package api
package server

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package api
package server

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/legacy_server.go → pkg/server/legacy_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//

package api
package server

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/max_bytes.go → pkg/server/max_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//

package api
package server

import "net/http"

Expand Down
2 changes: 1 addition & 1 deletion pkg/api/max_bytes_test.go → pkg/server/max_bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//

package api
package server

import (
"io/ioutil"
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/metrics.go → pkg/server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//

package api
package server

import (
"github.com/prometheus/client_golang/prometheus"
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/version.go → pkg/server/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
//

package api
package server

import (
"encoding/json"
Expand Down

0 comments on commit 1c17c94

Please sign in to comment.