Skip to content

Commit

Permalink
feat: upgrades masking to use env
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrltrent committed Aug 10, 2023
1 parent 2662c02 commit fce18f9
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ cat env-example > .env

Open the `.env` file and follow the [link](https://generate-random.org/api-token-generator) to create the `APPCHECK_TOKEN` env variable.

**Ensure you have the correct 16-byte `MASK_SECRET` in the `.env` file.**

An example is provided in the `env-example`, but obviously generate your own for prod.

**Add your AWS data to the `.env` file.**

Specifically what IAM roles are needed will be determined in the future. Currently, a general admin user suffices.
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ services:
AWS_ACCESS_KEY_ID:
AWS_SECRET_ACCESS_KEY:
AWS_REGION:
MASK_SECRET:

redis:
image: redis:alpine
Expand Down
3 changes: 3 additions & 0 deletions env-example
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ APPCHECK_TOKEN="kXfeSRgYTnoUztu6MO8FndqiRayoBaJqyDKQmoqvX3V9sZVlep/cm7cP!mgd-B9H
# either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256
HKDF_SECRET="some-secret-string"

# a 16-byte key
MASK_SECRET="your_16_byte_key"

# to generate a new nonce for production:
# go run ./scripts/main.go --new-nonce
CIPHER_NONCE="47363bcf91a1545668f0fd7a"
Expand Down
4 changes: 2 additions & 2 deletions features/schools/get_ranked.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ func (h *handler) handleGetRankedSchools(c *gin.Context) {
if len(ids) > 0 {
cleanedIds := make([]string, len(ids))
for i, id := range ids {
cleanedIds[i] = strings.Trim(id, "{}") // Remove curly braces
cleanedIds[i] = strings.Trim(id, "{}") // remove curly braces
}
idsStr := strings.Join(cleanedIds, ", ") // Convert the cleaned ids slice to a comma-separated string
idsStr := strings.Join(cleanedIds, ", ") // convert the cleaned ids slice to a comma-separated string
possibleRestriction = "WHERE s.id NOT IN (" + idsStr + ")"
fmt.Println(possibleRestriction)
}
Expand Down
13 changes: 11 additions & 2 deletions lib/masking/masking.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ import (
"encoding/base64"
"fmt"
"io"
"os"
"strconv"
)

// todo: .ENV
var secretKey = []byte("your_16_byte_key")
var secretKey []byte

func init() {
// load from .env
m := os.Getenv("MASK_SECRET")
if m == "" {
panic("MASK_SECRET env not found")
}
secretKey = []byte(m)
}

func Mask(id uint) (string, error) {
block, err := aes.NewCipher(secretKey)
Expand Down
2 changes: 2 additions & 0 deletions lib/masking/masking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/stretchr/testify/assert"
)

// Tests require `MASK_SECRET` env var to be set to pass

func TestUniqueMasksMapToSameID(t *testing.T) {
// Test case: Masking the same ID twice should result in different encrypted values
id := uint(5)
Expand Down

0 comments on commit fce18f9

Please sign in to comment.