Skip to content
Snippets Groups Projects
Unverified Commit 3c1d3dcf authored by dlorenc's avatar dlorenc Committed by GitHub
Browse files

Merge pull request #80 from dlorenc/ctx

Move API initialization to the package init, and access to a package …
parents 39948696 49484747
No related branches found
No related tags found
No related merge requests found
...@@ -64,8 +64,6 @@ jobs: ...@@ -64,8 +64,6 @@ jobs:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Docker Build - name: Docker Build
run: docker-compose build run: docker-compose build
- name: Docker Compose Start
run: docker-compose up -d
- uses: actions/setup-go@v2 - uses: actions/setup-go@v2
with: with:
go-version: ${{ env.GOVERSION }} go-version: ${{ env.GOVERSION }}
......
...@@ -18,6 +18,7 @@ package app ...@@ -18,6 +18,7 @@ package app
import ( import (
"github.com/go-openapi/loads" "github.com/go-openapi/loads"
"github.com/projectrekor/rekor/pkg/api"
"github.com/projectrekor/rekor/pkg/generated/restapi/operations" "github.com/projectrekor/rekor/pkg/generated/restapi/operations"
"github.com/projectrekor/rekor/pkg/log" "github.com/projectrekor/rekor/pkg/log"
"github.com/projectrekor/rekor/pkg/types/rekord" "github.com/projectrekor/rekor/pkg/types/rekord"
...@@ -58,6 +59,8 @@ var serveCmd = &cobra.Command{ ...@@ -58,6 +59,8 @@ var serveCmd = &cobra.Command{
server.Host = viper.GetString("rekor_server.address") server.Host = viper.GetString("rekor_server.address")
server.Port = int(viper.GetUint("rekor_server.port")) server.Port = int(viper.GetUint("rekor_server.port"))
api.ConfigureAPI()
server.ConfigureAPI() server.ConfigureAPI()
if err := server.Serve(); err != nil { if err := server.Serve(); err != nil {
log.Logger.Fatal(err) log.Logger.Fatal(err)
......
version: '3.1' version: '3.1'
services: services:
mysql: mysql:
image: gcr.io/trillian-opensource-ci/db_server image: gcr.io/trillian-opensource-ci/db_server:3c8193ebb2d7fedb44d18e9c810d0d2e4dbb7e4d
environment: environment:
- MYSQL_ROOT_PASSWORD=zaphod - MYSQL_ROOT_PASSWORD=zaphod
- MYSQL_DATABASE=test - MYSQL_DATABASE=test
......
...@@ -81,21 +81,20 @@ func NewAPI() (*API, error) { ...@@ -81,21 +81,20 @@ func NewAPI() (*API, error) {
}, nil }, nil
} }
type ctxKeyRekorAPI int var (
api *API
const rekorAPILookupKey ctxKeyRekorAPI = 0 )
func AddAPIToContext(ctx context.Context, api *API) context.Context {
return context.WithValue(ctx, rekorAPILookupKey, api)
}
func NewTrillianClient(ctx context.Context) *TrillianClient { func ConfigureAPI() {
api := ctx.Value(rekorAPILookupKey).(*API) var err error
if api == nil { api, err = NewAPI()
return nil if err != nil {
log.Logger.Panic(err)
} }
}
return &TrillianClient{ func NewTrillianClient(ctx context.Context) TrillianClient {
return TrillianClient{
client: api.logClient, client: api.logClient,
logID: api.logID, logID: api.logID,
context: ctx, context: ctx,
......
...@@ -43,9 +43,6 @@ import ( ...@@ -43,9 +43,6 @@ import (
func GetLogEntryByIndexHandler(params entries.GetLogEntryByIndexParams) middleware.Responder { func GetLogEntryByIndexHandler(params entries.GetLogEntryByIndexParams) middleware.Responder {
tc := NewTrillianClient(params.HTTPRequest.Context()) tc := NewTrillianClient(params.HTTPRequest.Context())
if tc == nil {
return handleRekorAPIError(params, http.StatusInternalServerError, errors.New("unable to get client from request context"), trillianCommunicationError)
}
resp := tc.getLeafByIndex(params.LogIndex) resp := tc.getLeafByIndex(params.LogIndex)
switch resp.status { switch resp.status {
...@@ -86,9 +83,6 @@ func CreateLogEntryHandler(params entries.CreateLogEntryParams) middleware.Respo ...@@ -86,9 +83,6 @@ func CreateLogEntryHandler(params entries.CreateLogEntryParams) middleware.Respo
} }
tc := NewTrillianClient(httpReq.Context()) tc := NewTrillianClient(httpReq.Context())
if tc == nil {
return handleRekorAPIError(params, http.StatusInternalServerError, errors.New("unable to get client from request context"), trillianCommunicationError)
}
resp := tc.addLeaf(leaf) resp := tc.addLeaf(leaf)
switch resp.status { switch resp.status {
...@@ -119,9 +113,6 @@ func GetLogEntryByUUIDHandler(params entries.GetLogEntryByUUIDParams) middleware ...@@ -119,9 +113,6 @@ func GetLogEntryByUUIDHandler(params entries.GetLogEntryByUUIDParams) middleware
hashes := [][]byte{hashValue} hashes := [][]byte{hashValue}
tc := NewTrillianClient(params.HTTPRequest.Context()) tc := NewTrillianClient(params.HTTPRequest.Context())
if tc == nil {
return handleRekorAPIError(params, http.StatusInternalServerError, errors.New("unable to get client from request context"), trillianCommunicationError)
}
resp := tc.getLeafByHash(hashes) // TODO: if this API is deprecated, we need to ask for inclusion proof and then use index in proof result to get leaf resp := tc.getLeafByHash(hashes) // TODO: if this API is deprecated, we need to ask for inclusion proof and then use index in proof result to get leaf
switch resp.status { switch resp.status {
...@@ -154,9 +145,6 @@ func GetLogEntryByUUIDHandler(params entries.GetLogEntryByUUIDParams) middleware ...@@ -154,9 +145,6 @@ func GetLogEntryByUUIDHandler(params entries.GetLogEntryByUUIDParams) middleware
func GetLogEntryProofHandler(params entries.GetLogEntryProofParams) middleware.Responder { func GetLogEntryProofHandler(params entries.GetLogEntryProofParams) middleware.Responder {
hashValue, _ := hex.DecodeString(params.EntryUUID) hashValue, _ := hex.DecodeString(params.EntryUUID)
tc := NewTrillianClient(params.HTTPRequest.Context()) tc := NewTrillianClient(params.HTTPRequest.Context())
if tc == nil {
return handleRekorAPIError(params, http.StatusInternalServerError, errors.New("unable to get client from request context"), trillianCommunicationError)
}
resp := tc.getProofByHash(hashValue) resp := tc.getProofByHash(hashValue)
switch resp.status { switch resp.status {
...@@ -202,9 +190,6 @@ func SearchLogQueryHandler(params entries.SearchLogQueryParams) middleware.Respo ...@@ -202,9 +190,6 @@ func SearchLogQueryHandler(params entries.SearchLogQueryParams) middleware.Respo
httpReqCtx := params.HTTPRequest.Context() httpReqCtx := params.HTTPRequest.Context()
resultPayload := []models.LogEntry{} resultPayload := []models.LogEntry{}
tc := NewTrillianClient(httpReqCtx) tc := NewTrillianClient(httpReqCtx)
if tc == nil {
return handleRekorAPIError(params, http.StatusInternalServerError, errors.New("unable to get client from request context"), trillianCommunicationError)
}
//TODO: parallelize this into different goroutines to speed up search //TODO: parallelize this into different goroutines to speed up search
searchHashes := [][]byte{} searchHashes := [][]byte{}
......
...@@ -35,9 +35,6 @@ import ( ...@@ -35,9 +35,6 @@ import (
func GetLogInfoHandler(params tlog.GetLogInfoParams) middleware.Responder { func GetLogInfoHandler(params tlog.GetLogInfoParams) middleware.Responder {
tc := NewTrillianClient(params.HTTPRequest.Context()) tc := NewTrillianClient(params.HTTPRequest.Context())
if tc == nil {
return handleRekorAPIError(params, http.StatusInternalServerError, errors.New("unable to get client from request context"), trillianCommunicationError)
}
resp := tc.getLatest(0) resp := tc.getLatest(0)
if resp.status != codes.OK { if resp.status != codes.OK {
...@@ -71,9 +68,6 @@ func GetLogProofHandler(params tlog.GetLogProofParams) middleware.Responder { ...@@ -71,9 +68,6 @@ func GetLogProofHandler(params tlog.GetLogProofParams) middleware.Responder {
return handleRekorAPIError(params, http.StatusBadRequest, nil, fmt.Sprintf(firstSizeLessThanLastSize, *params.FirstSize, params.LastSize)) return handleRekorAPIError(params, http.StatusBadRequest, nil, fmt.Sprintf(firstSizeLessThanLastSize, *params.FirstSize, params.LastSize))
} }
tc := NewTrillianClient(params.HTTPRequest.Context()) tc := NewTrillianClient(params.HTTPRequest.Context())
if tc == nil {
return handleRekorAPIError(params, http.StatusInternalServerError, errors.New("unable to get client from request context"), trillianCommunicationError)
}
resp := tc.getConsistencyProof(*params.FirstSize, params.LastSize) resp := tc.getConsistencyProof(*params.FirstSize, params.LastSize)
if resp.status != codes.OK { if resp.status != codes.OK {
......
...@@ -113,8 +113,6 @@ func setupGlobalMiddleware(handler http.Handler) http.Handler { ...@@ -113,8 +113,6 @@ func setupGlobalMiddleware(handler http.Handler) http.Handler {
returnHandler = middleware.Logger(returnHandler) returnHandler = middleware.Logger(returnHandler)
returnHandler = middleware.Heartbeat("/ping")(returnHandler) returnHandler = middleware.Heartbeat("/ping")(returnHandler)
// add the Trillian API object in context for all endpoints
returnHandler = addTrillianAPI(handler)
return middleware.RequestID(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return middleware.RequestID(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
r = r.WithContext(log.WithRequestID(ctx, middleware.GetReqID(ctx))) r = r.WithContext(log.WithRequestID(ctx, middleware.GetReqID(ctx)))
...@@ -138,17 +136,6 @@ func cacheForever(handler http.Handler) http.Handler { ...@@ -138,17 +136,6 @@ func cacheForever(handler http.Handler) http.Handler {
}) })
} }
func addTrillianAPI(handler http.Handler) http.Handler {
api, err := pkgapi.NewAPI()
if err != nil {
log.Logger.Panic(err)
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apiCtx := pkgapi.AddAPIToContext(r.Context(), api)
handler.ServeHTTP(w, r.WithContext(apiCtx))
})
}
func logAndServeError(w http.ResponseWriter, r *http.Request, err error) { func logAndServeError(w http.ResponseWriter, r *http.Request, err error) {
log.RequestIDLogger(r).Error(err) log.RequestIDLogger(r).Error(err)
requestFields := map[string]interface{}{} requestFields := map[string]interface{}{}
......
...@@ -2,8 +2,10 @@ ...@@ -2,8 +2,10 @@
set -ex set -ex
testdir=$(dirname "$0") testdir=$(dirname "$0")
docker-compose up -d
# Node # Node
nodedir=${testdir}/node nodedir=${testdir}/node
go run ./cmd/cli/ upload \ go run ./cmd/cli/ upload \
--artifact $(< ${nodedir}/url) --sha $(< ${nodedir}/sha) \ --artifact $(< ${nodedir}/url) --sha $(< ${nodedir}/sha) \
--signature=${nodedir}/sig --public-key=${nodedir}/key --signature=${nodedir}/sig --public-key=${nodedir}/key
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment