func (t authorizedKeyType) CreateKey(raw utils.RawMessage) (KeyLocator, error) { var value string if err := json.Unmarshal(raw, &value); err != nil { return nil, errors.New("The key value must be a string in the authorized_keys format.") } pk, _, _, _, ok := key.ParseAuthorizedKey([]byte(value)) if !ok { return nil, errors.New("Unable to parse the provided key") } contents := key.MarshalAuthorizedKey(pk) fingerprint := KeyFingerprint(pk) path := publicKeyPathFor(fingerprint) if err := utils.AtomicWriteToContentPath(path, 0664, contents); err != nil { return nil, err } return &SimpleKeyLocator{path, fingerprint.ToShortName()}, nil }
func readAuthorizedKeysFile(keyFile string) ([]jobs.KeyData, error) { var ( data []byte keys []jobs.KeyData err error ) // keyFile - contains the sshd AuthorizedKeysFile location // Stdin - contains the AuthorizedKeysFile if keyFile is not specified if len(keyFile) != 0 { absPath, _ := filepath.Abs(keyFile) data, err = ioutil.ReadFile(absPath) if err != nil { return keys, err } } else { data, _ = ioutil.ReadAll(os.Stdin) } bytesReader := bytes.NewReader(data) scanner := bufio.NewScanner(bytesReader) for scanner.Scan() { // Parse the AuthorizedKeys line pk, _, _, _, ok := sshkey.ParseAuthorizedKey(scanner.Bytes()) if !ok { return keys, errors.New("Unable to parse authorized key from input source, invalid format") } value := sshkey.MarshalAuthorizedKey(pk) key, err := jobs.NewKeyData("authorized_keys", string(value)) if err != nil { return keys, err } keys = append(keys, *key) } return keys, nil }