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

feat: secure api key #57

Merged
merged 2 commits into from
Apr 28, 2023
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
20 changes: 15 additions & 5 deletions core/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ var AUTH_KEY = "AUTH_KEY"

type AuthServer struct {
authServerUrl string
authToken string
}

func NewAuthServer(authServerUrl string) *AuthServer {
return &AuthServer{authServerUrl: authServerUrl}
func NewAuthServer(authServerUrl string, authToken string) *AuthServer {
return &AuthServer{authServerUrl: authServerUrl, authToken: authToken}
}

func (as *AuthServer) AuthMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
Expand All @@ -29,7 +30,7 @@ func (as *AuthServer) AuthMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return c.JSON(401, err.Error())
}

res, err := as.checkAuthToken(*authKey)
res, err := as.checkEstuaryAuthToken(*authKey)
if err != nil {
return c.JSON(401, err.Error())
}
Expand All @@ -38,10 +39,14 @@ func (as *AuthServer) AuthMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return c.JSON(401, res.Details)
}

valid := as.checkLocalAuthToken(*authKey)
if !valid {
return c.JSON(401, "this auth key is not permitted to access this instance of DDM")
}

c.Set(AUTH_KEY, *authKey)

return next(c)

}
}

Expand Down Expand Up @@ -69,7 +74,7 @@ func extractAuthKey(authorizationString string) (*string, error) {
}

// Makes a request to the auth server to check if a token is valid
func (as *AuthServer) checkAuthToken(token string) (*AuthResult, error) {
func (as *AuthServer) checkEstuaryAuthToken(token string) (*AuthResult, error) {
rqBody := strings.NewReader(fmt.Sprintf(`{"token": "%s"}`, token))
resp, err := http.Post(as.authServerUrl+"/check-api-key", "application/json", rqBody)
if err != nil {
Expand All @@ -95,6 +100,11 @@ func (as *AuthServer) checkAuthToken(token string) (*AuthResult, error) {
return &ar.Result, nil
}

// Check the local DB to see if a token is valid
func (as *AuthServer) checkLocalAuthToken(token string) bool {
return token == as.authToken
}

type AuthResponse struct {
Result AuthResult `json:"result"`
}
Expand Down
2 changes: 1 addition & 1 deletion core/ldm.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewDeltaDM(dbConnStr string, deltaApi string, authToken string, authServerU
log.Debugf("successfully connected to api at %s\n", deltaApi)
}

as := NewAuthServer(authServerUrl)
as := NewAuthServer(authServerUrl, authToken)

return &DeltaDM{
DAPI: dapi,
Expand Down
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ All endpoints are prefixed with `/api/v1`.

For example, `http://localhost:1314/api/v1/datasets`

All endpoints (with the exception of `/self-service`) require the `Authorization: Bearer <XXX>` header present on the request. Set this to the Delta API key.
All endpoints (with the exception of `/self-service`) require the `Authorization: Bearer <XXX>` header present on the request. It must match the `Delta API Key` that is passed into `delta-dm daemon` in order to be permitted to make requests.

## /health

Expand Down