Skip to content

Commit

Permalink
Merge branch 'beta/1.5.0' into cmd-master
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlHembrough committed Apr 10, 2018
2 parents df30620 + 21dd18a commit 2755f64
Show file tree
Hide file tree
Showing 26 changed files with 1,548 additions and 311 deletions.
7 changes: 7 additions & 0 deletions Dockerfile.concourse
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM onsdigital/dp-concourse-tools-ubuntu

WORKDIR /app/

ADD dp-dataset-api .

CMD ./dp-dataset-api
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ one of:
| GENERATE_DOWNLOADS_TOPIC | "filter-job-submitted" | The topic to send generate full dataset version downloads to
| HEALTHCHECK_TIMEOUT | 2s | The timeout that the healthcheck allows for checked subsystems
| ENABLE_PRIVATE_ENDPOINTS | false | Enable private endpoints for the API
| DOWNLOAD_SERVICE_SECRET_KEY | "QB0108EZ-825D-412C-9B1D-41EF7747F462" | A key specific for the download service to access public/private links
| DOWNLOAD_SERVICE_SECRET_KEY | QB0108EZ-825D-412C-9B1D-41EF7747F462 | A key specific for the download service to access public/private links
| ZEBEDEE_URL | http://localhost:8082 | The host name for Zebedee

### Contributing

Expand Down
57 changes: 30 additions & 27 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"context"
"time"

"github.com/ONSdigital/dp-dataset-api/auth"
"github.com/ONSdigital/dp-dataset-api/config"
"github.com/ONSdigital/dp-dataset-api/dimension"
"github.com/ONSdigital/dp-dataset-api/instance"
"github.com/ONSdigital/dp-dataset-api/store"
"github.com/ONSdigital/dp-dataset-api/url"
"github.com/ONSdigital/go-ns/identity"
"github.com/ONSdigital/go-ns/log"
"github.com/ONSdigital/go-ns/server"
"github.com/gorilla/mux"
"github.com/justinas/alice"
)

var httpServer *server.Server
Expand All @@ -31,22 +32,30 @@ type DownloadsGenerator interface {
type DatasetAPI struct {
dataStore store.DataStore
host string
zebedeeURL string
internalToken string
downloadServiceToken string
EnablePrePublishView bool
privateAuth *auth.Authenticator
router *mux.Router
urlBuilder *url.Builder
downloadGenerator DownloadsGenerator
healthCheckTimeout time.Duration
serviceAuthToken string
}

// CreateDatasetAPI manages all the routes configured to API
func CreateDatasetAPI(cfg config.Configuration, dataStore store.DataStore, urlBuilder *url.Builder, errorChan chan error, downloadsGenerator DownloadsGenerator) {
router := mux.NewRouter()
routes(cfg, router, dataStore, urlBuilder, downloadsGenerator)

httpServer = server.New(cfg.BindAddr, router)
// Only add the identity middleware when running in publishing.
if cfg.EnablePrivateEnpoints {
alice := alice.New(identity.Handler(true, cfg.ZebedeeURL)).Then(router)
httpServer = server.New(cfg.BindAddr, alice)
} else {
httpServer = server.New(cfg.BindAddr, router)
}

// Disable this here to allow main to manage graceful shutdown of the entire app.
httpServer.HandleOSSignals = false

Expand All @@ -61,17 +70,11 @@ func CreateDatasetAPI(cfg config.Configuration, dataStore store.DataStore, urlBu

func routes(cfg config.Configuration, router *mux.Router, dataStore store.DataStore, urlBuilder *url.Builder, downloadGenerator DownloadsGenerator) *DatasetAPI {

var authenticator *auth.Authenticator

if cfg.EnablePrivateEnpoints {
authenticator = &auth.Authenticator{SecretKey: cfg.SecretKey, HeaderName: "Internal-Token"}
}

api := DatasetAPI{
privateAuth: authenticator,
dataStore: dataStore,
host: cfg.DatasetAPIURL,
internalToken: cfg.SecretKey,
zebedeeURL: cfg.ZebedeeURL,
serviceAuthToken: cfg.ServiceAuthToken,
downloadServiceToken: cfg.DownloadServiceSecretKey,
EnablePrePublishView: cfg.EnablePrivateEnpoints,
router: router,
Expand All @@ -97,29 +100,29 @@ func routes(cfg config.Configuration, router *mux.Router, dataStore store.DataSt
log.Debug("private endpoints have been enabled", nil)

versionPublishChecker := PublishCheck{Datastore: dataStore.Backend}
api.router.HandleFunc("/datasets/{id}", api.privateAuth.Check(api.addDataset)).Methods("POST")
api.router.HandleFunc("/datasets/{id}", api.privateAuth.Check(api.putDataset)).Methods("PUT")
api.router.HandleFunc("/datasets/{id}", api.privateAuth.Check(api.deleteDataset)).Methods("DELETE")
api.router.HandleFunc("/datasets/{id}/editions/{edition}/versions/{version}", api.privateAuth.Check(versionPublishChecker.Check(api.putVersion))).Methods("PUT")
api.router.HandleFunc("/datasets/{id}", identity.Check(api.addDataset)).Methods("POST")
api.router.HandleFunc("/datasets/{id}", identity.Check(api.putDataset)).Methods("PUT")
api.router.HandleFunc("/datasets/{id}", identity.Check(api.deleteDataset)).Methods("DELETE")
api.router.HandleFunc("/datasets/{id}/editions/{edition}/versions/{version}", identity.Check(versionPublishChecker.Check(api.putVersion))).Methods("PUT")

instanceAPI := instance.Store{Host: api.host, Storer: api.dataStore.Backend}
instancePublishChecker := instance.PublishCheck{Datastore: dataStore.Backend}
api.router.HandleFunc("/instances", api.privateAuth.Check(instanceAPI.GetList)).Methods("GET")
api.router.HandleFunc("/instances", api.privateAuth.Check(instanceAPI.Add)).Methods("POST")
api.router.HandleFunc("/instances/{id}", api.privateAuth.Check(instanceAPI.Get)).Methods("GET")
api.router.HandleFunc("/instances/{id}", api.privateAuth.Check(instancePublishChecker.Check(instanceAPI.Update))).Methods("PUT")
api.router.HandleFunc("/instances/{id}/dimensions/{dimension}", api.privateAuth.Check(instancePublishChecker.Check(instanceAPI.UpdateDimension))).Methods("PUT")
api.router.HandleFunc("/instances/{id}/events", api.privateAuth.Check(instanceAPI.AddEvent)).Methods("POST")
api.router.HandleFunc("/instances", identity.Check(instanceAPI.GetList)).Methods("GET")
api.router.HandleFunc("/instances", identity.Check(instanceAPI.Add)).Methods("POST")
api.router.HandleFunc("/instances/{id}", identity.Check(instanceAPI.Get)).Methods("GET")
api.router.HandleFunc("/instances/{id}", identity.Check(instancePublishChecker.Check(instanceAPI.Update))).Methods("PUT")
api.router.HandleFunc("/instances/{id}/dimensions/{dimension}", identity.Check(instancePublishChecker.Check(instanceAPI.UpdateDimension))).Methods("PUT")
api.router.HandleFunc("/instances/{id}/events", identity.Check(instanceAPI.AddEvent)).Methods("POST")
api.router.HandleFunc("/instances/{id}/inserted_observations/{inserted_observations}",
api.privateAuth.Check(instancePublishChecker.Check(instanceAPI.UpdateObservations))).Methods("PUT")
api.router.HandleFunc("/instances/{id}/import_tasks", api.privateAuth.Check(instancePublishChecker.Check(instanceAPI.UpdateImportTask))).Methods("PUT")
identity.Check(instancePublishChecker.Check(instanceAPI.UpdateObservations))).Methods("PUT")
api.router.HandleFunc("/instances/{id}/import_tasks", identity.Check(instancePublishChecker.Check(instanceAPI.UpdateImportTask))).Methods("PUT")

dimension := dimension.Store{Storer: api.dataStore.Backend}
api.router.HandleFunc("/instances/{id}/dimensions", api.privateAuth.Check(dimension.GetNodes)).Methods("GET")
api.router.HandleFunc("/instances/{id}/dimensions", api.privateAuth.Check(instancePublishChecker.Check(dimension.Add))).Methods("POST")
api.router.HandleFunc("/instances/{id}/dimensions/{dimension}/options", api.privateAuth.Check(dimension.GetUnique)).Methods("GET")
api.router.HandleFunc("/instances/{id}/dimensions", identity.Check(dimension.GetNodes)).Methods("GET")
api.router.HandleFunc("/instances/{id}/dimensions", identity.Check(instancePublishChecker.Check(dimension.Add))).Methods("POST")
api.router.HandleFunc("/instances/{id}/dimensions/{dimension}/options", identity.Check(dimension.GetUnique)).Methods("GET")
api.router.HandleFunc("/instances/{id}/dimensions/{dimension}/options/{value}/node_id/{node_id}",
api.privateAuth.Check(instancePublishChecker.Check(dimension.AddNodeID))).Methods("PUT")
identity.Check(instancePublishChecker.Check(dimension.AddNodeID))).Methods("PUT")
}
return &api
}
Expand Down
Loading

0 comments on commit 2755f64

Please sign in to comment.