github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/state/sshhostkeys.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package state 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/names" 9 "gopkg.in/mgo.v2" 10 "gopkg.in/mgo.v2/bson" 11 "gopkg.in/mgo.v2/txn" 12 ) 13 14 // SSHHostKeys holds the public SSH host keys for an entity (almost 15 // certainly a machine). 16 // 17 // The host keys are one line each and are stored in the same format 18 // as the SSH authorized_keys and ssh_host_key*.pub files. 19 type SSHHostKeys []string 20 21 // sshHostKeysDoc represents the MongoDB document that stores the SSH 22 // host keys for an entity. 23 // 24 // Note that the document id hasn't been included because we don't 25 // need to read it or (directly) write it. 26 type sshHostKeysDoc struct { 27 Keys []string `bson:"keys"` 28 } 29 30 // GetSSHHostKeys retrieves the SSH host keys stored for an entity. 31 /// 32 // NOTE: Currently only machines are supported. This can be 33 // generalised to take other tag types later, if and when we need it. 34 func (st *State) GetSSHHostKeys(tag names.MachineTag) (SSHHostKeys, error) { 35 coll, closer := st.getCollection(sshHostKeysC) 36 defer closer() 37 38 var doc sshHostKeysDoc 39 err := coll.FindId(machineGlobalKey(tag.Id())).One(&doc) 40 if err == mgo.ErrNotFound { 41 return nil, errors.NotFoundf("SSH host keys for %s", tag) 42 } else if err != nil { 43 return nil, errors.Annotate(err, "SSH host key lookup failed") 44 } 45 return SSHHostKeys(doc.Keys), nil 46 } 47 48 // SetSSHHostKeys updates the stored SSH host keys for an entity. 49 // 50 // See the note for GetSSHHostKeys regarding supported entities. 51 func (st *State) SetSSHHostKeys(tag names.MachineTag, keys SSHHostKeys) error { 52 id := machineGlobalKey(tag.Id()) 53 doc := sshHostKeysDoc{ 54 Keys: keys, 55 } 56 err := st.runTransaction([]txn.Op{ 57 { 58 C: sshHostKeysC, 59 Id: id, 60 Insert: doc, 61 }, { 62 C: sshHostKeysC, 63 Id: id, 64 Update: bson.M{"$set": doc}, 65 }, 66 }) 67 return errors.Annotate(err, "SSH host key update failed") 68 } 69 70 // removeSSHHostKeyOp returns the operation needed to remove the SSH 71 // host key document associated with the given globalKey. 72 func removeSSHHostKeyOp(st *State, globalKey string) txn.Op { 73 return txn.Op{ 74 C: sshHostKeysC, 75 Id: globalKey, 76 Remove: true, 77 } 78 }