confz

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2024 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

zero-security-conf GoDoc

A simple method for config encrypt/decrypt by AES-256 for go-zero.

Usage

Add confz.SecurityConf to your Config.

import (
	"github.com/zeromicro/go-zero/rest"
	confz "github.com/WqyJh/zero-security-conf"
)
type Config struct {
	rest.RestConf
 	Security confz.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.

import (
	"github.com/WqyJh/confcrypt"
)

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 confz.SecurityLoad or confz.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.

License

Released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SecurityLoad

func SecurityLoad(path string, v interface{}, opts ...conf.Option) error

func SecurityMustLoad

func SecurityMustLoad(path string, v interface{}, opts ...conf.Option)

Types

type SecurityConf

type SecurityConf struct {
	Enable bool   `json:",default=true"`
	Env    string `json:",default=CONFIG_KEY"` // environment variable name stores the encryption key
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址