summaryrefslogtreecommitdiff
path: root/client/client.go
blob: d6a34871319ff3d60de2feca8df51e559e9803a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package client

import (
	"context"
	"fmt"
	"sync"

	"git.tardisproject.uk/tcmal/vault-plugin-kerberos-secrets/config"
)

type client struct {
	*sync.Mutex

	config *config.Config
}

func ClientFromConfig(config *config.Config) (client, error) {
	return client{
		&sync.Mutex{},
		config,
	}, nil
}

func (c client) SetPassword(ctx context.Context, username string, password string) error {
	c.Lock()
	defer c.Unlock()

	// check if the principal exists
	exists, err := c.princExists(ctx, username)
	if err != nil {
		return fmt.Errorf("error checking principal exists: %s", err)
	}

	if !exists {
		// if not, create it
		err = c.doCreatePrinc(ctx, username, password)
	} else {
		// otherwise, just set the password
		err = c.doChangePassword(ctx, username, password)
	}

	return err
}