Skip to content

Opaque encrypted token from account API responses #47

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

Merged
merged 1 commit into from
Oct 6, 2024
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
17 changes: 16 additions & 1 deletion src/controller/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (controller *AccountController) CreateAccountHandler(w http.ResponseWriter,
gitService := core.NewGitService(accountCreated.Repository, accountCreated.Token, accountCreated.Branch)
go controller.coreHandler.StartAccountHandler(accountCreated.ID.Hex(), gitService)

opaqueTokenFromResponse(accountCreated)
utils.ResponseJSON(w, accountCreated, http.StatusCreated)
}

Expand All @@ -78,6 +79,7 @@ func (controller *AccountController) FetchAccountHandler(w http.ResponseWriter,
return
}

opaqueTokenFromResponse(account)
utils.ResponseJSON(w, account, http.StatusOK)
}

Expand All @@ -91,7 +93,13 @@ func (controller *AccountController) FetchAllAccountsByDomainIdHandler(w http.Re
return
}

utils.ResponseJSON(w, accounts, http.StatusOK)
var accountsResponse []model.Account
for _, account := range accounts {
opaqueTokenFromResponse(&account)
accountsResponse = append(accountsResponse, account)
}

utils.ResponseJSON(w, accountsResponse, http.StatusOK)
}

func (controller *AccountController) UpdateAccountHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -115,6 +123,7 @@ func (controller *AccountController) UpdateAccountHandler(w http.ResponseWriter,
return
}

opaqueTokenFromResponse(accountUpdated)
utils.ResponseJSON(w, accountUpdated, http.StatusOK)
}

Expand All @@ -131,3 +140,9 @@ func (controller *AccountController) DeleteAccountHandler(w http.ResponseWriter,

utils.ResponseJSON(w, nil, http.StatusNoContent)
}

func opaqueTokenFromResponse(account *model.Account) {
if account.Token != "" {
account.Token = "..." + account.Token[len(account.Token)-4:]
}
}
15 changes: 9 additions & 6 deletions src/controller/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/switcherapi/switcher-gitops/src/config"
"github.com/switcherapi/switcher-gitops/src/model"
"github.com/switcherapi/switcher-gitops/src/repository"
"github.com/switcherapi/switcher-gitops/src/utils"
)

Expand All @@ -29,9 +30,7 @@ func TestCreateAccountHandler(t *testing.T) {
assert.Equal(t, http.StatusCreated, response.Code)
assert.Nil(t, err)
assert.Equal(t, accountV1.Repository, accountResponse.Repository)

token, _ := utils.Decrypt(accountResponse.Token, config.GetEnv("GIT_TOKEN_PRIVATE_KEY"))
assert.Equal(t, accountV1.Token, token)
assert.Contains(t, accountResponse.Token, "...")
})

t.Run("Should not create an account - invalid request", func(t *testing.T) {
Expand Down Expand Up @@ -78,6 +77,7 @@ func TestFetchAccountHandler(t *testing.T) {
assert.Equal(t, http.StatusOK, response.Code)
assert.Nil(t, err)
assert.Equal(t, accountV1.Repository, accountResponse.Repository)
assert.Contains(t, accountResponse.Token, "...")
})

t.Run("Should not fetch an account by domain ID / environment - not found", func(t *testing.T) {
Expand Down Expand Up @@ -110,6 +110,8 @@ func TestFetchAccountHandler(t *testing.T) {
assert.Equal(t, http.StatusOK, response.Code)
assert.Nil(t, err)
assert.Equal(t, 2, len(accountsResponse))
assert.Contains(t, accountsResponse[0].Token, "...")
assert.Contains(t, accountsResponse[1].Token, "...")
})

t.Run("Should not fetch all accounts by domain ID - not found", func(t *testing.T) {
Expand Down Expand Up @@ -174,11 +176,12 @@ func TestUpdateAccountHandler(t *testing.T) {
assert.Equal(t, http.StatusOK, response.Code)
assert.Nil(t, err)
assert.Equal(t, accountV1.Repository, accountResponse.Repository)
assert.Contains(t, accountResponse.Token, "...")

encryptedToken := utils.Encrypt(accountV1.Token, config.GetEnv("GIT_TOKEN_PRIVATE_KEY"))
assert.NotEqual(t, encryptedToken, accountResponse.Token)
accountRepository := repository.NewAccountRepositoryMongo(mongoDb)
accountFromDb, _ := accountRepository.FetchByAccountId(accountResponse.ID.Hex())

decryptedToken, _ := utils.Decrypt(accountResponse.Token, config.GetEnv("GIT_TOKEN_PRIVATE_KEY"))
decryptedToken, _ := utils.Decrypt(accountFromDb.Token, config.GetEnv("GIT_TOKEN_PRIVATE_KEY"))
assert.Equal(t, "new-token", decryptedToken)
})

Expand Down