Skip to content

Commit

Permalink
Merge pull request #10 from fanux/terminal
Browse files Browse the repository at this point in the history
Terminal
  • Loading branch information
cuisongliu authored Mar 10, 2019
2 parents b88887b + da8bec5 commit f34c9ba
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
68 changes: 68 additions & 0 deletions auth/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package auth

import (
"errors"
"github.com/fanux/fist/tools"
)

// vars
var AdminUsername string
var AdminPassword string

// consts
const (
DefaultNamespace = "sealyun"
DefaultSecretName = "fist-admin"
)

type Admin struct {
Name string
Passwd string
}

type Adminer interface {
LoadSecret() error
IsAdmin() (bool, error)
}

func NewAdmin(name string, passwd string) Adminer {
var admire Adminer
admire = &Admin{Name: name, Passwd: passwd}
return admire
}

func (*Admin) LoadSecret() error {
clients, err := tools.GetK8sClient()
if err != nil {
return err
}
if AdminUsername == "" {
secrets, err := tools.GetSecrets(DefaultNamespace, DefaultSecretName, clients)
if err != nil {
return err
}
AdminUsername = string(secrets.Data["username"])
}
if AdminPassword == "" {
secrets, err := tools.GetSecrets(DefaultNamespace, DefaultSecretName, clients)
if err != nil {
return err
}
AdminPassword = string(secrets.Data["password"])
}
return nil
}

func (admin *Admin) IsAdmin() (bool, error) {
if admin.Name == "" {
return false, errors.New("the username is empty")
}
if admin.Passwd == "" {
return false, errors.New("the password is empty")
}
if admin.Name == AdminUsername && admin.Passwd == AdminPassword {
return true, nil
} else {
return false, errors.New("the username and password is mismatching")
}
}
45 changes: 45 additions & 0 deletions auth/admin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package auth

import (
"strconv"
"testing"
)

func TestAdmin_IsAdminFalse(t *testing.T) {
adminer := NewAdmin("admin", "admin")
err := adminer.LoadSecret()
if err != nil {
panic(err)
return
}
isAdmin, err := adminer.IsAdmin()
println("isAdmin:" + strconv.FormatBool(isAdmin))
if err != nil {
panic(err)
return
}
}

func TestAdmin_IsAdminTrue(t *testing.T) {
adminer := NewAdmin("admin", "1f2d1e2e67df")
err := adminer.LoadSecret()
if err != nil {
panic(err)
return
}
isAdmin, err := adminer.IsAdmin()
println("isAdmin:" + strconv.FormatBool(isAdmin))
if err != nil {
panic(err)
return
}
}

func TestAdmin_LoadSecret(t *testing.T) {
adminer := NewAdmin("admin", "admin")
err := adminer.LoadSecret()
if err != nil {
panic(err)
return
}
}
9 changes: 9 additions & 0 deletions auth/deploy/secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: Secret
metadata:
name: fist-admin
namespace: sealyun
type: Opaque
data:
username: YWRtaW4=
password: MWYyZDFlMmU2N2Rm

0 comments on commit f34c9ba

Please sign in to comment.