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

Feature/cognito webpage redirect #149

Merged
merged 54 commits into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
db4e3bc
Added code assets for display_credentials_web_page lambda
joshmarsh Nov 20, 2019
41d6091
Expire time now shows up in encoded auth creds
joshmarsh Nov 20, 2019
77c11c0
Added copy to clipboard button
joshmarsh Nov 21, 2019
ba35d3f
Added auth endpoint to terraform
joshmarsh Nov 25, 2019
4023cea
Rewrote static web server lambda in golang
joshmarsh Nov 25, 2019
8853769
Added unit test for status code 404
joshmarsh Nov 25, 2019
5cf9b2e
Setting content-type for static assets
joshmarsh Nov 25, 2019
2a3a673
WIP: Solved cognito cyclical dependency issues using ssm
joshmarsh Nov 26, 2019
a5d8997
Merging master
joshmarsh Nov 26, 2019
0ed320c
Setting env vars from parameter store
joshmarsh Nov 27, 2019
25a82e6
Removed some unnecessary code
joshmarsh Nov 27, 2019
b35e29d
Add /auth to end of callback url
joshmarsh Nov 27, 2019
f6cd88f
build script bug
joshmarsh Nov 29, 2019
f3ecacf
build script bug
joshmarsh Nov 29, 2019
2552c87
/auth x-amazon-apigateway-integration GET -> POST
joshmarsh Nov 29, 2019
17f17ee
Merge branch 'master' into feature/cognito-webpage-redirect
joshmarsh Nov 29, 2019
023baad
Moved repeated response writing code to api/response pkg
joshmarsh Dec 1, 2019
c07f5c9
Added functionality to config to make ssm env vars work
joshmarsh Dec 2, 2019
d97a9fc
linting
joshmarsh Dec 2, 2019
12e3ea7
TF Formatting
joshmarsh Dec 2, 2019
126c6a2
Go Formatting
joshmarsh Dec 2, 2019
8362038
Added .idea to gitignore
joshmarsh Dec 2, 2019
c3b7149
Untracked .idea
joshmarsh Dec 2, 2019
728cb41
Removed print statements
joshmarsh Dec 2, 2019
3a2b154
Removed unused mock
joshmarsh Dec 2, 2019
abffb62
Updated config.Dump docs
joshmarsh Dec 2, 2019
d36b156
Minor doc changes
joshmarsh Dec 2, 2019
dc7f575
Functional test bug
joshmarsh Dec 2, 2019
f664ee4
Moved SSM param retrieval to AWSServiceBuilder.Build()
joshmarsh Dec 2, 2019
9dee3f9
Go formatting
joshmarsh Dec 2, 2019
fb1d84a
functional test bug
joshmarsh Dec 2, 2019
dd239b9
Debugging functional test error
joshmarsh Dec 2, 2019
25bd098
Removed print statement
joshmarsh Dec 2, 2019
0c57c01
Removed unnecessary error check
joshmarsh Dec 3, 2019
3a2827e
Updated CHANGELOG
joshmarsh Dec 3, 2019
c9343bd
Mapping admin groups/claims to admin role
joshmarsh Dec 3, 2019
6d50df4
Merge branch 'master' into feature/cognito-webpage-redirect
joshmarsh Dec 4, 2019
75060b3
UserDetailer nil pointer bug
joshmarsh Dec 5, 2019
d019e8b
Removed lease_auth binary
joshmarsh Dec 5, 2019
d560445
formatting
joshmarsh Dec 5, 2019
c506a38
Merge branch 'master' into feature/cognito-webpage-redirect
joshmarsh Dec 5, 2019
2501d5e
fixed test failure
joshmarsh Dec 5, 2019
6717d5a
Added UserDetails to controller_test
joshmarsh Dec 5, 2019
ef189f3
Giving users permission to post to leases/*
joshmarsh Dec 5, 2019
aa5733e
Added sign out button to creds page
joshmarsh Dec 5, 2019
b8fd367
Refresh bug fix
joshmarsh Dec 6, 2019
1938f55
Added minified vuejs
joshmarsh Dec 6, 2019
620bc8b
Fix missing hyphen in Cognito user pool name
eschwartz Dec 10, 2019
fabc35b
Merge branch 'feature/cognito-webpage-redirect' of github.com:Optum/d…
joshmarsh Dec 10, 2019
84fb508
Removed JQuery
joshmarsh Dec 10, 2019
d9b886e
Added newline after creds string
joshmarsh Dec 10, 2019
60efcbb
Merge branch 'master' into feature/cognito-webpage-redirect
joshmarsh Dec 10, 2019
e8c8cec
Removed profile from scopes
joshmarsh Dec 12, 2019
91fb6fe
Merge branch 'master' into feature/cognito-webpage-redirect
joshmarsh Dec 12, 2019
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- Fixed bug where child account's DCEPrincipal role trusted itself rather than the master account
- Add GetUsageByPrincipal
- Fix default `budget_notification_from_email` TF var (See #143)

- Add `GET /auth` and `GET /auth/{file+}` endpoints for retrieving credentials web page

## v0.23.0

Expand Down
24 changes: 12 additions & 12 deletions cmd/lambda/accounts/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func CreateAccount(w http.ResponseWriter, r *http.Request) {
err = decoder.Decode(&request)

if err != nil {
WriteAPIErrorResponse(w, http.StatusBadRequest, "ClientError", "invalid request parameters")
response.WriteAPIErrorResponse(w, http.StatusBadRequest, "ClientError", "invalid request parameters")
return
}

Expand All @@ -41,7 +41,7 @@ func CreateAccount(w http.ResponseWriter, r *http.Request) {
// Validate the request body
isValid, validationRes := request.Validate()
if !isValid {
WriteAPIErrorResponse(w, http.StatusBadRequest, "ClientError", *validationRes)
response.WriteAPIErrorResponse(w, http.StatusBadRequest, "ClientError", *validationRes)
return
}

Expand All @@ -50,11 +50,11 @@ func CreateAccount(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Printf("Failed to add account %s to pool: %s",
request.ID, err.Error())
WriteAPIErrorResponse(w, http.StatusInternalServerError, "ServerError", "")
response.WriteAPIErrorResponse(w, http.StatusInternalServerError, "ServerError", "")
return
}
if existingAccount != nil {
WriteAlreadyExistsError(w)
response.WriteAlreadyExistsError(w)
return
}

Expand All @@ -66,7 +66,7 @@ func CreateAccount(w http.ResponseWriter, r *http.Request) {
})

if err != nil {
WriteRequestValidationError(
response.WriteRequestValidationError(
w,
fmt.Sprintf("Unable to add account %s to pool: adminRole is not assumable by the master account", request.ID),
)
Expand All @@ -89,7 +89,7 @@ func CreateAccount(w http.ResponseWriter, r *http.Request) {
createRolRes, policyHash, err := createPrincipalRole(account, masterAccountID)
if err != nil {
log.Printf("failed to create principal role for %s: %s", request.ID, err)
WriteServerErrorWithResponse(w, "Internal server error")
response.WriteServerErrorWithResponse(w, "Internal server error")
return
}
account.PrincipalRoleArn = createRolRes.RoleArn
Expand All @@ -100,15 +100,15 @@ func CreateAccount(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Printf("Failed to add account %s to pool: %s",
request.ID, err.Error())
WriteServerErrorWithResponse(w, "Internal server error")
response.WriteServerErrorWithResponse(w, "Internal server error")
return
}

// Add Account to Reset Queue
err = Queue.SendMessage(&resetQueueURL, &account.ID)
if err != nil {
log.Printf("Failed to add account %s to reset Queue: %s", account.ID, err)
WriteServerErrorWithResponse(w, "Internal server error")
response.WriteServerErrorWithResponse(w, "Internal server error")
return
}

Expand All @@ -117,7 +117,7 @@ func CreateAccount(w http.ResponseWriter, r *http.Request) {
snsMessage, err := common.PrepareSNSMessageJSON(accountResponse)
if err != nil {
log.Printf("Failed to create SNS account-created message for %s: %s", account.ID, err)
WriteServerErrorWithResponse(w, "Internal server error")
response.WriteServerErrorWithResponse(w, "Internal server error")
return
}

Expand All @@ -126,18 +126,18 @@ func CreateAccount(w http.ResponseWriter, r *http.Request) {
_, err = SnsSvc.PublishMessage(&accountCreatedTopicArn, &snsMessage, true)
if err != nil {
log.Printf("Failed to publish SNS account-created message for %s: %s", account.ID, err)
WriteServerErrorWithResponse(w, "Internal server error")
response.WriteServerErrorWithResponse(w, "Internal server error")
return
}

accountResponseJSON, err := json.Marshal(accountResponse)
if err != nil {
log.Printf("ERROR: Failed to marshal account response for %s: %s", account.ID, err)
WriteServerErrorWithResponse(w, "Internal server error")
response.WriteServerErrorWithResponse(w, "Internal server error")
return
}

WriteAPIResponse(
response.WriteAPIResponse(
w,
http.StatusCreated,
string(accountResponseJSON),
Expand Down
10 changes: 5 additions & 5 deletions cmd/lambda/accounts/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ func DeleteAccount(w http.ResponseWriter, r *http.Request) {
if err != nil {
switch err.(type) {
case *db.AccountNotFoundError:
WriteNotFoundError(w)
response.WriteNotFoundError(w)
return
case *db.AccountLeasedError:
WriteAPIErrorResponse(
response.WriteAPIErrorResponse(
w,
http.StatusConflict,
"Conflict",
err.Error(),
)
return
default:
WriteServerErrorWithResponse(w, "Internal Server Error")
response.WriteServerErrorWithResponse(w, "Internal Server Error")
return
}
}
Expand All @@ -49,8 +49,8 @@ func DeleteAccount(w http.ResponseWriter, r *http.Request) {
// Push the account to the Reset Queue, so it gets cleaned up
sendToResetQueue(deletedAccount.ID)

// json.NewEncoder(w).Encode(response.CreateAPIResponse(http.StatusNoContent, ""))
WriteAPIResponse(w, http.StatusNoContent, "")
// json.NewEncoder(w).Encode(response.CreateAPIGatewayResponse(http.StatusNoContent, ""))
response.WriteAPIResponse(w, http.StatusNoContent, "")
}

// sendSNS sends notification to SNS that the delete has occurred.
Expand Down
10 changes: 5 additions & 5 deletions cmd/lambda/accounts/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func GetAllAccounts(w http.ResponseWriter, r *http.Request) {
if err != nil {
errorMessage := fmt.Sprintf("Failed to query database: %s", err)
log.Print(errorMessage)
WriteServerErrorWithResponse(w, errorMessage)
response.WriteServerErrorWithResponse(w, errorMessage)
}

// Serialize them for the JSON response.
Expand All @@ -43,12 +43,12 @@ func GetAccountByID(w http.ResponseWriter, r *http.Request) {
if err != nil {
errorMessage := fmt.Sprintf("Failed List on Account Lease %s", accountID)
log.Print(errorMessage)
WriteServerErrorWithResponse(w, errorMessage)
response.WriteServerErrorWithResponse(w, errorMessage)
return
}

if account == nil {
WriteNotFoundError(w)
response.WriteNotFoundError(w)
return
}

Expand All @@ -68,11 +68,11 @@ func GetAccountByStatus(w http.ResponseWriter, r *http.Request) {
if err != nil {
errorMessage := fmt.Sprintf("Failed to query database: %s", err)
log.Print(errorMessage)
WriteServerErrorWithResponse(w, errorMessage)
response.WriteServerErrorWithResponse(w, errorMessage)
}

if len(accounts) == 0 {
WriteNotFoundError(w)
response.WriteNotFoundError(w)
return
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/lambda/accounts/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (controller listController) Call(ctx context.Context, req *events.APIGatewa
if err != nil {
errorMessage := fmt.Sprintf("Failed to query database: %s", err)
log.Print(errorMessage)
return response.CreateAPIErrorResponse(http.StatusInternalServerError,
return response.CreateAPIGatewayErrorResponse(http.StatusInternalServerError,
response.CreateErrorResponse(
"ServerError", errorMessage)), nil
}
Expand All @@ -42,12 +42,12 @@ func (controller listController) Call(ctx context.Context, req *events.APIGatewa
if err != nil {
errorMessage := fmt.Sprintf("Failed to serialize data: %s", err)
log.Print(errorMessage)
return response.CreateAPIErrorResponse(http.StatusInternalServerError,
return response.CreateAPIGatewayErrorResponse(http.StatusInternalServerError,
response.CreateErrorResponse(
"ServerError", errorMessage)), nil
}

body := string(messageBytes)

return response.CreateAPIResponse(http.StatusOK, body), nil
return response.CreateAPIGatewayResponse(http.StatusOK, body), nil
}
68 changes: 0 additions & 68 deletions cmd/lambda/accounts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ package main

import (
"context"
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iam"
"log"
"net/http"
"strings"

"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"

"github.com/Optum/dce/pkg/api/response"
"github.com/Optum/dce/pkg/common"
"github.com/Optum/dce/pkg/rolemanager"
"github.com/aws/aws-sdk-go/aws/session"
Expand Down Expand Up @@ -187,68 +184,3 @@ func newAWSSession() *session.Session {
}
return awsSession
}

// WriteServerErrorWithResponse - Writes a server error with the specific message.
func WriteServerErrorWithResponse(w http.ResponseWriter, message string) {
Copy link
Contributor Author

@joshmarsh joshmarsh Dec 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are used by accounts, usage, and credentials_web_page, so I moved them here to be more DRY.

WriteAPIErrorResponse(
w,
http.StatusInternalServerError,
"ServerError",
message,
)
}

// WriteAPIErrorResponse - Writes the error response out to the provided ResponseWriter
func WriteAPIErrorResponse(w http.ResponseWriter, responseCode int,
errCode string, errMessage string) {
// Create the Error Response
errResp := response.CreateErrorResponse(errCode, errMessage)
apiResponse, err := json.Marshal(errResp)

// Should most likely not return an error since response.ErrorResponse
// is structured to be json compatible
if err != nil {
log.Printf("Failed to Create Valid Error Response: %s", err)
WriteAPIResponse(w, http.StatusInternalServerError, fmt.Sprintf(
"{\"error\":\"Failed to Create Valid Error Response: %s\"", err))
}

// Write an error
WriteAPIResponse(w, responseCode, string(apiResponse))
}

// WriteAPIResponse - Writes the response out to the provided ResponseWriter
func WriteAPIResponse(w http.ResponseWriter, status int, body string) {
w.WriteHeader(status)
w.Write([]byte(body))
}

// WriteAlreadyExistsError - Writes the already exists error.
func WriteAlreadyExistsError(w http.ResponseWriter) {
WriteAPIErrorResponse(
w,
http.StatusConflict,
"AlreadyExistsError",
"The requested resource cannot be created, as it conflicts with an existing resource",
)
}

// WriteRequestValidationError - Writes a request validate error with the given message.
func WriteRequestValidationError(w http.ResponseWriter, message string) {
WriteAPIErrorResponse(
w,
http.StatusBadRequest,
"RequestValidationError",
message,
)
}

// WriteNotFoundError - Writes a request validate error with the given message.
func WriteNotFoundError(w http.ResponseWriter) {
WriteAPIErrorResponse(
w,
http.StatusNotFound,
"NotFound",
"The requested resource could not be found.",
)
}
14 changes: 7 additions & 7 deletions cmd/lambda/accounts/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func UpdateAccountByID(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&request)
if err != nil {
WriteAPIErrorResponse(w, http.StatusBadRequest, "ClientError", "invalid request parameters")
response.WriteAPIErrorResponse(w, http.StatusBadRequest, "ClientError", "invalid request parameters")
return
}
request.ID = &accountID
Expand All @@ -50,7 +50,7 @@ func UpdateAccountByID(w http.ResponseWriter, r *http.Request) {
})

if err != nil {
WriteRequestValidationError(
response.WriteRequestValidationError(
w,
fmt.Sprintf("Unable to update account %s: "+
"admin role is not assumable by the master account",
Expand Down Expand Up @@ -78,7 +78,7 @@ func UpdateAccountByID(w http.ResponseWriter, r *http.Request) {
accountPartial.Metadata = *request.Metadata
}
if len(fieldsToUpdate) == 0 {
WriteRequestValidationError(
response.WriteRequestValidationError(
w,
fmt.Sprintf("Unable to update account %s: "+
"no updatable fields provided",
Expand All @@ -92,23 +92,23 @@ func UpdateAccountByID(w http.ResponseWriter, r *http.Request) {
if err != nil {
// If the account doesn't exist, return a 404
if _, ok := err.(*db.NotFoundError); ok {
WriteNotFoundError(w)
response.WriteNotFoundError(w)
return
}
// Other DB errors return a 500
log.Printf("ERROR: Failed to update account %s: %s", *request.ID, err)
WriteServerErrorWithResponse(w, "Internal Server Error")
response.WriteServerErrorWithResponse(w, "Internal Server Error")
return
}

accountJSON, err := json.Marshal(response.AccountResponse(*acct))
if err != nil {
log.Printf("ERROR: Failed to marshal account response for %s: %s", *request.ID, err)
WriteServerErrorWithResponse(w, "Internal server error")
response.WriteServerErrorWithResponse(w, "Internal server error")
return
}

WriteAPIResponse(
response.WriteAPIResponse(
w,
http.StatusOK,
string(accountJSON),
Expand Down
Binary file not shown.
49 changes: 49 additions & 0 deletions cmd/lambda/credentials_web_page/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"fmt"
"github.com/Optum/dce/pkg/api/response"
"html/template"
"log"
"net/http"
"path/filepath"
"strings"
)

func GetAuthPage(w http.ResponseWriter, r *http.Request) {
lp := filepath.Join("views", "index.html")

tmpl, err := template.ParseFiles(lp)
if err != nil {
errorMessage := fmt.Sprintf("Failed to load web page: %s", err)
log.Print(errorMessage)
response.WriteServerErrorWithResponse(w, errorMessage)
}
if err := tmpl.Execute(w, Config); err != nil {
errorMessage := fmt.Sprintf("Failed to load web page: %s", err)
log.Print(errorMessage)
response.WriteServerErrorWithResponse(w, errorMessage)
}
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
}

func GetAuthPageAssets(w http.ResponseWriter, r *http.Request) {
fs := http.FileServer(http.Dir("./public"))
sp := http.StripPrefix("/auth/public", fs)

splitStr := strings.Split(r.URL.Path, ".")
ext := splitStr[len(splitStr)-1]
var contentType string
switch ext {
case "css":
contentType = "text/css"
case "js":
contentType = "text/javascript"
default:
contentType = "application/json"
}

w.Header().Set("Content-Type", contentType)
sp.ServeHTTP(w, r)
}
Loading