-
Notifications
You must be signed in to change notification settings - Fork 2
/
service_factory.go
72 lines (58 loc) · 1.98 KB
/
service_factory.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package server
import (
"crypto/x509"
"encoding/pem"
"strings"
"time"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh"
"github.com/dpb587/ssoca/certauth"
"github.com/dpb587/ssoca/config"
"github.com/dpb587/ssoca/server/service"
"github.com/dpb587/ssoca/server/service/dynamicvalue"
svc "github.com/dpb587/ssoca/service/ssh"
svcconfig "github.com/dpb587/ssoca/service/ssh/server/config"
)
type ServiceFactory struct {
svc.ServiceType
caManager certauth.Manager
dynamicvalueFactory dynamicvalue.Factory
}
var _ service.ServiceFactory = ServiceFactory{}
func NewServiceFactory(dynamicvalueFactory dynamicvalue.Factory, caManager certauth.Manager) ServiceFactory {
return ServiceFactory{
caManager: caManager,
dynamicvalueFactory: dynamicvalueFactory,
}
}
func (f ServiceFactory) Create(name string, options map[string]interface{}) (service.Service, error) {
var cfg svcconfig.Config
cfg.Validity = 2 * time.Minute
cfg.CertAuth = certauth.NewConfigValue(f.caManager)
cfg.Extensions = svcconfig.ExtensionDefaults
cfg.Principals = dynamicvalue.NewMultiConfigValue(f.dynamicvalueFactory)
cfg.CriticalOptions = svcconfig.NewCriticalOptions(f.dynamicvalueFactory)
cfg.Target = svcconfig.Target{
User: dynamicvalue.NewConfigValue(f.dynamicvalueFactory),
}
err := config.RemarshalYAML(options, &cfg)
if err != nil {
return nil, errors.Wrap(err, "loading config")
}
if cfg.Target.PublicKey != "" && strings.Contains(cfg.Target.PublicKey, "-----") {
publicKeyPEM, _ := pem.Decode([]byte(cfg.Target.PublicKey))
if publicKeyPEM == nil {
return nil, errors.New("failed to parse public key")
}
rsa, err := x509.ParsePKIXPublicKey(publicKeyPEM.Bytes)
if err != nil {
return nil, errors.Wrap(err, "parsing public key")
}
publicKey, err := ssh.NewPublicKey(rsa)
if err != nil {
return nil, errors.Wrap(err, "parsing ssh public key")
}
cfg.Target.PublicKey = string(ssh.MarshalAuthorizedKey(publicKey))
}
return NewService(name, cfg), nil
}