/
main.go
182 lines (142 loc) · 4.59 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"os"
"syscall"
"github.com/go-logr/zapr"
"go.uber.org/zap"
cmdutil "github.com/kubermatic/kubermatic/api/cmd/util"
usersshkeys "github.com/kubermatic/kubermatic/api/pkg/controller/usersshkeysagent"
kubermaticlog "github.com/kubermatic/kubermatic/api/pkg/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client/config"
ctrlruntimelog "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
func main() {
logOpts := kubermaticlog.NewDefaultOptions()
logOpts.AddFlags(flag.CommandLine)
flag.Parse()
rawLog := kubermaticlog.New(logOpts.Debug, logOpts.Format)
log := rawLog.Sugar()
cmdutil.Hello(log, "User SSH-Key Agent", logOpts.Debug)
cfg, err := config.GetConfig()
if err != nil {
log.Fatalw("Failed getting user cluster controller config", zap.Error(err))
}
ctx, ctxDone := context.WithCancel(context.Background())
defer ctxDone()
// Create Context
done := ctx.Done()
ctrlruntimelog.Log = ctrlruntimelog.NewDelegatingLogger(zapr.NewLogger(rawLog).WithName("controller_runtime"))
mgr, err := manager.New(cfg, manager.Options{Namespace: metav1.NamespaceSystem})
if err != nil {
log.Fatalw("Failed creating user ssh key controller", zap.Error(err))
}
paths, err := availableUsersPaths()
if err != nil {
log.Fatalw("Failed to get users directories", zap.Error(err))
}
if err := usersshkeys.Add(mgr, log, paths); err != nil {
log.Fatalw("Failed registering user ssh key controller", zap.Error(err))
}
if err := mgr.Start(done); err != nil {
log.Fatalw("error occurred while running the controller manager", zap.Error(err))
}
}
func availableUsersPaths() ([]string, error) {
var paths []string
users, err := availableHomeUsers()
if err != nil {
return nil, fmt.Errorf("failed to get users in the home dir: %v", err)
}
for _, user := range users {
path := fmt.Sprintf("/%v", user)
if user != "root" {
path = fmt.Sprintf("/home%v", path)
}
fileInfo, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
continue
}
return nil, fmt.Errorf("failed describing file info: %v", err)
}
uid := fileInfo.Sys().(*syscall.Stat_t).Uid
gid := fileInfo.Sys().(*syscall.Stat_t).Gid
sshPath := fmt.Sprintf("%v/.ssh", path)
if err := createDirIfNotExist(sshPath, int(uid), int(gid)); err != nil {
return nil, err
}
authorizedKeysPath := fmt.Sprintf("%v/authorized_keys", sshPath)
if err := createFileIfNotExist(authorizedKeysPath, int(uid), int(gid)); err != nil {
return nil, err
}
paths = append(paths, authorizedKeysPath)
}
return paths, nil
}
func createDirIfNotExist(path string, uid, gid int) error {
_, err := os.Stat(path)
if err == nil {
return nil
}
if !os.IsNotExist(err) {
return fmt.Errorf("failed describing file info: %v", err)
}
if err := os.Mkdir(path, 0700); err != nil {
return fmt.Errorf("failed creating .ssh dir in %s: %v", path, err)
}
if err := os.Chown(path, uid, gid); err != nil {
return fmt.Errorf("failed changing the numeric uid and gid of %s: %v", path, err)
}
return nil
}
func createFileIfNotExist(path string, uid, gid int) error {
_, err := os.Stat(path)
if err == nil {
return nil
}
if !os.IsNotExist(err) {
return fmt.Errorf("failed describing file info: %v", err)
}
if _, err := os.Create(path); err != nil {
return fmt.Errorf("failed creating authorized_keys file in %s: %v", path, err)
}
if err := os.Chmod(path, os.FileMode(0600)); err != nil {
return fmt.Errorf("failed changing file mode for authorized_keys file in %s: %v", path, err)
}
if err := os.Chown(path, uid, gid); err != nil {
return fmt.Errorf("failed changing the numeric uid and gid of %s: %v", path, err)
}
return nil
}
func availableHomeUsers() ([]string, error) {
files, err := ioutil.ReadDir("/home")
if err != nil {
return nil, err
}
var users = []string{"root"}
for _, file := range files {
if !file.IsDir() {
continue
}
users = append(users, file.Name())
}
return users, nil
}