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: add security conf #3751

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

WqyJh
Copy link
Contributor

@WqyJh WqyJh commented Nov 29, 2023

A simple method for config encrypt/decrypt.

Add conf.SecurityConf to your Config.

import (
	"github.com/zeromicro/go-zero/rest"
	"github.com/zeromicro/go-zero/core/conf"
)
type Config struct {
	rest.RestConf
 	Security conf.SecurityConf
 	SensitiveKey string
 	SensitiveValue string
}

Use the following code to encrypt your sensitive data, and replace the plain string in your config with the encrypted string.

var (
	key = "12345678"
)

func TestEncrypt(t *testing.T) {
	plain := "sensitive_key"
	encrypted, err := confcrypt.EncryptString(plain, key)
	assert.NoError(t, err)
	t.Logf("encrypted: '%s'", encrypted) // encrypted: ENC~i1eiPez4IICS/iA+zIEyDk3UHQz9enP+kHG3X/LCJixtgEw4i3o=
}

This is the config file.

Security:
  Enable: true
  Env: MY_SECRET_KEY

SensitiveKey: ENC~i1eiPez4IICS/iA+zIEyDk3UHQz9enP+kHG3X/LCJixtgEw4i3o=
SensitiveValue: ENC~KWLH5csxSeG3zgPMFYmgIslTrPaWUfZsLaAkJ9z9zwf6LXyHh5ddYeO5sCRH8xeLOXGWUaA=

Use conf.SecurityLoad or conf.SecurityMustLoad instead of conf.Load or conf.MustLoad.

Start the service with environment variable of you secret key.

export MY_SECRET_KEY=mysecretkey

All of the string config starts with ENC~ would be decrypted.

@kevwan kevwan added the kind/need-more-discussion Not decided, need more discussion! label Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/need-more-discussion Not decided, need more discussion!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants