2015-01-25 06:32:33 +03:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// 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.
|
2014-10-18 02:36:22 +04:00
|
|
|
|
2014-03-18 20:00:41 +04:00
|
|
|
package system
|
2014-03-05 04:36:05 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-03-26 10:23:27 +03:00
|
|
|
"os"
|
2014-03-05 04:36:05 +04:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Add the provide SSH public key to the core user's list of
|
|
|
|
// authorized keys
|
2014-03-13 21:56:59 +04:00
|
|
|
func AuthorizeSSHKeys(user string, keysName string, keys []string) error {
|
2015-03-26 10:23:27 +03:00
|
|
|
for name, key := range keys {
|
|
|
|
keys[name] = strings.TrimSpace(key)
|
2014-03-05 04:36:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// join all keys with newlines, ensuring the resulting string
|
|
|
|
// also ends with a newline
|
|
|
|
joined := fmt.Sprintf("%s\n", strings.Join(keys, "\n"))
|
|
|
|
|
2015-03-26 10:23:27 +03:00
|
|
|
authorized_file := ""
|
|
|
|
switch user {
|
|
|
|
case "root":
|
|
|
|
authorized_file = "/root/.ssh/authorized_keys"
|
|
|
|
default:
|
|
|
|
authorized_file = fmt.Sprintf("/home/%s/.ssh/authorized_keys", user)
|
2014-03-05 04:36:05 +04:00
|
|
|
}
|
|
|
|
|
2015-03-26 10:23:27 +03:00
|
|
|
f, err := os.OpenFile(authorized_file, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
2014-03-05 04:36:05 +04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-26 10:23:27 +03:00
|
|
|
defer f.Close()
|
|
|
|
_, err = f.WriteString(joined)
|
2014-03-05 04:36:05 +04:00
|
|
|
|
2015-03-26 10:23:27 +03:00
|
|
|
return err
|
2014-03-05 04:36:05 +04:00
|
|
|
}
|