cloudinit/system/ssh_key.go

109 lines
2.3 KiB
Go
Raw Normal View History

// 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-03-18 20:00:41 +04:00
package system
2014-03-05 04:36:05 +04:00
import (
"bufio"
2014-03-05 04:36:05 +04:00
"fmt"
"os"
2014-03-05 04:36:05 +04:00
"strings"
)
func diffLines(src, dst []string) []string {
var tgt []string
mb := map[string]bool{}
for _, x := range src {
mb[x] = true
}
for _, x := range dst {
if _, ok := mb[x]; !ok {
mb[x] = true
}
}
for k, _ := range mb {
tgt = append(tgt, k)
}
return tgt
}
func readLines(path string) ([]string, error) {
var lines []string
file, err := os.Open(path)
if err != nil {
return lines, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
// writeLines writes the lines to the given file.
func writeLines(lines []string, path string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
for _, line := range lines {
fmt.Fprintln(w, line)
}
return w.Flush()
}
2014-03-05 04:36:05 +04:00
// Add the provide SSH public key to the core user's list of
// authorized keys
func AuthorizeSSHKeys(user string, keysName string, keys []string) error {
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"))
2014-03-05 04:36:05 +04:00
home, err := UserHome(user)
if err != nil {
return err
2014-03-05 04:36:05 +04:00
}
if _, err = os.Stat(home + "/.ssh"); err != nil {
if err = os.MkdirAll(home+"/.ssh", os.FileMode(0755)); err != nil {
return err
}
}
authorized_file := fmt.Sprintf("%s/.ssh/authorized_keys", home)
var newkeys []string
for _, x := range keys {
newkeys = append(newkeys, strings.Split(x, "\n")...)
2014-03-05 04:36:05 +04:00
}
oldkeys, _ := readLines(authorized_file)
2014-03-05 04:36:05 +04:00
diffkeys := diffLines(oldkeys, newkeys)
return writeLines(diffkeys, authorized_file)
2014-03-05 04:36:05 +04:00
}