4848 ErrNoPermChangeAccount = errors .New ("can not change other account password" )
4949 ErrWrongPassword = errors .New ("current pwd is wrong" )
5050 ErrSamePassword = errors .New ("the password can not be same as old one" )
51+ ErrEmptyPassword = errors .New ("empty password" )
5152)
5253
5354//Init decide whether enable rbac function and save root account to db
@@ -116,16 +117,16 @@ func readPublicKey() {
116117}
117118func initFirstTime (admin string ) {
118119 //handle root account
119- pwd := archaius . GetString ( InitPassword , "" )
120- if pwd == "" {
120+ pwd , err := getPassword ( )
121+ if err != nil {
121122 log .Fatal ("can not enable rbac, password is empty" , nil )
122123 }
123124 a := & rbacframe.Account {
124125 Name : admin ,
125126 Password : pwd ,
126127 Role : rbacframe .RoleAdmin ,
127128 }
128- err : = service .ValidateCreateAccount (a )
129+ err = service .ValidateCreateAccount (a )
129130 if err != nil {
130131 log .Fatal ("invalid pwd" , err )
131132 return
@@ -140,6 +141,20 @@ func initFirstTime(admin string) {
140141 log .Info ("root account init success" )
141142}
142143
144+ func getPassword () (string , error ) {
145+ p := archaius .GetString (InitPassword , "" )
146+ if p == "" {
147+ log .Fatal ("can not enable rbac, password is empty" , nil )
148+ return "" , ErrEmptyPassword
149+ }
150+ d , err := cipher .Decrypt (p )
151+ if err != nil {
152+ log .Warn ("cipher fallback: " + err .Error ())
153+ return p , nil
154+ }
155+ return d , nil
156+ }
157+
143158func Enabled () bool {
144159 return beego .AppConfig .DefaultBool ("rbac_enabled" , false )
145160}
@@ -154,6 +169,7 @@ func privateKey() string {
154169 ep := archaius .GetString ("rbac_private_key" , "" )
155170 p , err := cipher .Decrypt (ep )
156171 if err != nil {
172+ log .Warn ("cipher fallback: " + err .Error ())
157173 return ep
158174 }
159175 return p
0 commit comments