This is a combination of 4 commits.
Fix the image
This commit is contained in:
		@@ -4,22 +4,30 @@ import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/google/uuid"
 | 
			
		||||
	"golang.org/x/crypto/bcrypt"
 | 
			
		||||
	corev1 "k8s.io/api/core/v1"
 | 
			
		||||
	rbacv1 "k8s.io/api/rbac/v1"
 | 
			
		||||
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
			
		||||
 | 
			
		||||
	"k8s.io/apimachinery/pkg/types"
 | 
			
		||||
	ctrl "sigs.k8s.io/controller-runtime"
 | 
			
		||||
	"sigs.k8s.io/controller-runtime/pkg/client"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Account struct {
 | 
			
		||||
	Controller ctrl.Manager
 | 
			
		||||
	Params AccountParams
 | 
			
		||||
	Data       *AccountData
 | 
			
		||||
	Kubeconfig string
 | 
			
		||||
	Token string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type AccountParams struct {
 | 
			
		||||
	HashCost int16
 | 
			
		||||
}
 | 
			
		||||
type AccountData struct {
 | 
			
		||||
	Username string
 | 
			
		||||
	Password string
 | 
			
		||||
@@ -28,7 +36,7 @@ type AccountData struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func HashPassword(password string) (string, error) {
 | 
			
		||||
	bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
 | 
			
		||||
	bytes, err := bcrypt.GenerateFromPassword([]byte(password), 1)
 | 
			
		||||
	return string(bytes), err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -37,21 +45,47 @@ func CheckPasswordHash(password, hash string) bool {
 | 
			
		||||
    return err == nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func waitUntilCreated(ctx context.Context, client client.Client ,obj client.Object, attemps int, timeout time.Duration) error {
 | 
			
		||||
	log.Printf("Waiting %d", attemps)
 | 
			
		||||
	if err := client.Get(ctx, types.NamespacedName{
 | 
			
		||||
		Namespace: obj.GetNamespace(),
 | 
			
		||||
		Name: obj.GetName(),
 | 
			
		||||
	}, obj); err != nil {
 | 
			
		||||
		if attemps > 0 {
 | 
			
		||||
			time.Sleep(timeout)
 | 
			
		||||
			waitUntilCreated(ctx, client, obj, attemps - 1, timeout)
 | 
			
		||||
		} else {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	} 
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (acc *Account) Create(ctx context.Context) error {
 | 
			
		||||
	client := acc.Controller.GetClient()
 | 
			
		||||
 | 
			
		||||
	acc.Data.UUID = uuid.New().String()
 | 
			
		||||
	log.Println(acc.Data.UUID)
 | 
			
		||||
	passwordHash, err := HashPassword(acc.Data.Password)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	namespace := corev1.Namespace{
 | 
			
		||||
		ObjectMeta: metav1.ObjectMeta{
 | 
			
		||||
			Name: acc.Data.UUID,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := client.Create(ctx, &namespace); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := waitUntilCreated(ctx, client, &namespace, 10, time.Millisecond * 50); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
	if err := client.Get(ctx, types.NamespacedName{
 | 
			
		||||
		Name:      acc.Data.UUID,
 | 
			
		||||
	}, &namespace); err != nil {
 | 
			
		||||
@@ -156,8 +190,11 @@ func (acc *Account) Create(ctx context.Context) error {
 | 
			
		||||
		}
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	if err := waitUntilCreated(ctx, client, saSec, 10, time.Millisecond * 50); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	acc.Kubeconfig, err = acc.getToken(ctx, saSec)
 | 
			
		||||
	acc.Token, err = acc.getToken(ctx, saSec)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		if err := client.Delete(ctx, &namespace); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
@@ -193,7 +230,7 @@ func (acc *Account) Login (ctx context.Context) error {
 | 
			
		||||
		Type:       "kubernetes.io/service-account-token",
 | 
			
		||||
	}
 | 
			
		||||
	var err error
 | 
			
		||||
	acc.Kubeconfig, err = acc.getToken(ctx, saSec)
 | 
			
		||||
	acc.Token, err = acc.getToken(ctx, saSec)
 | 
			
		||||
	if err != nil{ 
 | 
			
		||||
	return err
 | 
			
		||||
	}
 | 
			
		||||
@@ -210,3 +247,4 @@ func (acc *Account) getToken (ctx context.Context, saSec *corev1.Secret) (string
 | 
			
		||||
	}
 | 
			
		||||
	return string(saSec.Data["token"]), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										22
									
								
								internal/helpers/email/email.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								internal/helpers/email/email.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
			
		||||
package email
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"net/smtp"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type EmailConf struct {
 | 
			
		||||
	From string
 | 
			
		||||
	Password string
 | 
			
		||||
	SmtpHost string
 | 
			
		||||
	SmtpPort string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (e *EmailConf) SendEmail (to string, message string )error  {
 | 
			
		||||
	messageByte := []byte(message)
 | 
			
		||||
	auth := smtp.PlainAuth("", e.From, e.Password, e.SmtpHost)
 | 
			
		||||
 | 
			
		||||
	if err := smtp.SendMail(e.SmtpHost+":"+e.SmtpPort, auth, e.From, []string{to}, messageByte); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
} 
 | 
			
		||||
							
								
								
									
										14
									
								
								internal/helpers/hash/hash.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								internal/helpers/hash/hash.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
package hash
 | 
			
		||||
 | 
			
		||||
import "golang.org/x/crypto/bcrypt"
 | 
			
		||||
 | 
			
		||||
func HashPassword(password string, cost int) (string, error) {
 | 
			
		||||
	bytes, err := bcrypt.GenerateFromPassword([]byte(password), cost)
 | 
			
		||||
	return string(bytes), err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func CheckPasswordHash(password, hash string) bool {
 | 
			
		||||
    err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
 | 
			
		||||
    return err == nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										21
									
								
								internal/helpers/hash/hash_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								internal/helpers/hash/hash_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
package hash_test
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/hash"
 | 
			
		||||
	"github.com/alecthomas/assert/v2"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestHashValid(t *testing.T) {
 | 
			
		||||
    password := "qwertyu9"
 | 
			
		||||
    hpass, err := hash.HashPassword(password, 10)
 | 
			
		||||
    assert.NoError(t, err)
 | 
			
		||||
    assert.True(t, hash.CheckPasswordHash(password, hpass))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestHashInvalid(t *testing.T) {
 | 
			
		||||
    password := "qwertyu9"
 | 
			
		||||
    invhash := "qwertyu9"
 | 
			
		||||
    assert.False(t, hash.CheckPasswordHash(password, invhash))
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user