Skip to content

Commit

Permalink
add ldflags to go build binary (labring#3679)
Browse files Browse the repository at this point in the history
* add ldflags to go build binary

* make it easier to understand

* modify to secret.CONTROLLER_BUILD_CRYPTOKEY
  • Loading branch information
bxy4543 committed Aug 15, 2023
1 parent f03f37a commit 2bbd1dd
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/workflows/controllers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ env:
# Common versions
GO_VERSION: "1.20"
DEFAULT_OWNER: "labring"
CRYPTOKEY: ${{ secrets.CONTROLLER_BUILD_CRYPTOKEY }}
jobs:
resolve-modules:
runs-on: ubuntu-20.04
Expand Down
2 changes: 1 addition & 1 deletion controllers/account/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ test: manifests generate fmt vet envtest ## Run tests.

.PHONY: build
build: ## Build manager binary.
CGO_ENABLED=0 GOOS=linux go build -o bin/manager main.go
CGO_ENABLED=0 GOOS=linux go build $(shell [ -n "${CRYPTOKEY}" ] && echo "-ldflags '-X github.com/labring/sealos/controllers/pkg/crypto.encryptionKey=${CRYPTOKEY} -X github.com/labring/sealos/controllers/pkg/database.cryptoKey=${CRYPTOKEY}'") -o bin/manager main.go

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
Expand Down
4 changes: 2 additions & 2 deletions controllers/licenseissuer/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ test: manifests generate fmt vet envtest ## Run tests.
##@ Build

.PHONY: build
build: manifests generate fmt vet ## Build manager binary.
CGO_ENABLED=0 GOOS=linux go build -o bin/manager cmd/main.go
build:
CGO_ENABLED=0 GOOS=linux go build $(shell [ -n "${CRYPTOKEY}" ] && echo "-ldflags '-X github.com/labring/sealos/controllers/pkg/crypto.encryptionKey=${CRYPTOKEY} -X github.com/labring/sealos/controllers/pkg/database.cryptoKey=${CRYPTOKEY}'") -o bin/manager cmd/main.go

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
Expand Down
8 changes: 5 additions & 3 deletions controllers/pkg/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ import (
v1 "github.com/labring/sealos/controllers/licenseissuer/api/v1"
)

var encryptionKey = []byte("0123456789ABCDEF0123456789ABCDEF")
const defaultEncryptionKey = "0123456789ABCDEF0123456789ABCDEF"

var encryptionKey = defaultEncryptionKey

// Encrypt encrypts the given plaintext using AES-GCM.
func Encrypt(plaintext []byte) (string, error) {
return EncryptWithKey(plaintext, encryptionKey)
return EncryptWithKey(plaintext, []byte(encryptionKey))
}

// EncryptWithKey encrypts the given plaintext using AES-GCM.
Expand Down Expand Up @@ -119,7 +121,7 @@ func DeductBalance(balance *string, amount int64) error {

// Decrypt decrypts the given ciphertext using AES-GCM.
func Decrypt(ciphertextBase64 string) ([]byte, error) {
return DecryptWithKey(ciphertextBase64, encryptionKey)
return DecryptWithKey(ciphertextBase64, []byte(encryptionKey))
}

// DecryptWithKey decrypts the given ciphertext using AES-GCM.
Expand Down
6 changes: 4 additions & 2 deletions controllers/pkg/database/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ const (
MongoPassword = "MONGO_PASSWORD"
)

var cryptoKey = []byte("Af0b2Bc5e9d0C84adF0A5887cF43aB63")
const defaultCryptoKey = "Af0b2Bc5e9d0C84adF0A5887cF43aB63"

var cryptoKey = defaultCryptoKey

type MongoDB struct {
URL string
Expand Down Expand Up @@ -192,7 +194,7 @@ func (m *MongoDB) GetAllPricesMap() (map[string]common.Price, error) {
}
var pricesMap = make(map[string]common.Price, len(prices))
for i := range prices {
price, err := crypto.DecryptInt64WithKey(prices[i].Price, cryptoKey)
price, err := crypto.DecryptInt64WithKey(prices[i].Price, []byte(cryptoKey))
if err != nil {
return nil, fmt.Errorf("decrypt price error: %v", err)
}
Expand Down
3 changes: 1 addition & 2 deletions controllers/resources/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ test: manifests generate fmt vet envtest ## Run tests.

.PHONY: build
build: ## Build manager binary.
CGO_ENABLED=0 GOOS=linux go build -o bin/manager main.go
CGO_ENABLED=0 GOOS=linux go build -o metering/bin-metering metering/main.go
CGO_ENABLED=0 GOOS=linux go build $(shell [ -n "${CRYPTOKEY}" ] && echo "-ldflags '-X github.com/labring/sealos/controllers/pkg/crypto.encryptionKey=${CRYPTOKEY} -X github.com/labring/sealos/controllers/pkg/database.cryptoKey=${CRYPTOKEY}'") -o bin/manager main.go

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
Expand Down
2 changes: 1 addition & 1 deletion controllers/resources/metering/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ all: build

.PHONY: build
build: ## Build manager binary.
CGO_ENABLED=0 GOOS=linux go build -o bin/manager main.go
CGO_ENABLED=0 GOOS=linux go build $(shell [ -n "${CRYPTOKEY}" ] && echo "-ldflags '-X github.com/labring/sealos/controllers/pkg/crypto.encryptionKey=${CRYPTOKEY} -X github.com/labring/sealos/controllers/pkg/database.cryptoKey=${CRYPTOKEY}'") -o bin/manager main.go

.PHONY: run
run: ## Run a controller from your host.
Expand Down

0 comments on commit 2bbd1dd

Please sign in to comment.