2 # vim:set ts=4 sw=4 expandtab:
4 # NodeManager plugin for creating credentials in slivers
5 # (*) empower slivers to make API calls throught hmac
6 # (*) also create a ssh key - used by the OMF resource controller
7 # for authenticating itself with its Experiment Controller
8 # in order to avoid spamming the DB with huge amounts of such tags,
9 # (*) slices need to have the 'enable_hmac' tag set
10 # (*) or the 'omf_control' tag set, respectively
13 Sliver authentication support for NodeManager.
27 logger.log("sliverauth: (dummy) plugin starting up...")
29 def GetSlivers(data, config, plc):
30 if 'OVERRIDES' in dir(config):
31 if config.OVERRIDES.get('sliverauth') == '-1':
32 logger.log("sliverauth: Disabled", 2)
35 if 'slivers' not in data:
36 logger.log_missing_data("sliverauth.GetSlivers", 'slivers')
39 for sliver in data['slivers']:
40 path = '/vservers/%s' % sliver['name']
41 if not os.path.exists(path):
42 # ignore all non-plc-instantiated slivers
43 instantiation = sliver.get('instantiation','')
44 if instantiation == 'plc-instantiated':
45 logger.log("sliverauth: plc-instantiated slice %s does not yet exist. IGNORING!" % sliver['name'])
49 for chunk in sliver['attributes']:
50 if chunk['tagname'] == "system":
51 if chunk['value'] in (True, 1, '1') or chunk['value'].lower() == "true":
54 for chunk in sliver['attributes']:
55 if chunk['tagname']=='enable_hmac' and not system_slice:
56 manage_hmac (plc, sliver)
58 if chunk['tagname']=='omf_control':
59 manage_sshkey (plc, sliver)
62 def SetSliverTag(plc, slice, tagname, value):
63 node_id = tools.node_id()
64 slivertags=plc.GetSliceTags({"name":slice,"node_id":node_id,"tagname":tagname})
65 if len(slivertags)==0:
66 # looks like GetSlivers reports about delegated/nm-controller slices that do *not* belong to this node
67 # and this is something that AddSliceTag does not like
69 slivertag_id=plc.AddSliceTag(slice,tagname,value,node_id)
71 logger.log_exc ("sliverauth.SetSliverTag (probably delegated) slice=%(slice)s tag=%(tagname)s node_id=%(node_id)d"%locals())
74 slivertag_id=slivertags[0]['slice_tag_id']
75 plc.UpdateSliceTag(slivertag_id,value)
77 def find_tag (sliver, tagname):
78 for attribute in sliver['attributes']:
79 # for legacy, try the old-fashioned 'name' as well
80 name = attribute.get('tagname',attribute.get('name',''))
82 return attribute['value']
85 def manage_hmac (plc, sliver):
86 hmac = find_tag (sliver, 'hmac')
89 # let python do its thing
91 d = [random.choice(string.letters) for x in xrange(32)]
93 SetSliverTag(plc,sliver['name'],'hmac',hmac)
94 logger.log("sliverauth: %s: setting hmac" % sliver['name'])
96 path = '/vservers/%s/etc/planetlab' % sliver['name']
97 if os.path.exists(path):
98 keyfile = '%s/key' % path
99 if (tools.replace_file_with_string(keyfile,hmac,chmod=0400)):
100 logger.log ("sliverauth: (over)wrote hmac into %s " % keyfile)
102 # create the key if needed and returns the key contents
103 def generate_sshkey (sliver):
104 # initial version was storing stuff in the sliver directly
105 # keyfile="/vservers/%s/home/%s/.ssh/id_rsa"%(sliver['name'],sliver['name'])
106 # we're now storing this in the same place as the authorized_keys, which in turn
107 # gets mounted to the user's home directory in the sliver
108 keyfile="/home/%s/.ssh/id_rsa"%(sliver['name'])
109 pubfile="%s.pub"%keyfile
110 dotssh=os.path.dirname(keyfile)
111 # create dir if needed
112 if not os.path.isdir (dotssh):
113 os.mkdir (dotssh, 0700)
114 logger.log_call ( [ 'chown', "%s:slices"%(sliver['name']), dotssh ] )
115 if not os.path.isfile (pubfile):
116 comment="%s@%s"%(sliver['name'],socket.gethostname())
117 logger.log_call( [ 'ssh-keygen', '-t', 'rsa', '-N', '', '-f', keyfile , '-C', comment] )
118 os.chmod (keyfile, 0400)
119 logger.log_call ( [ 'chown', "%s:slices"%(sliver['name']), keyfile, pubfile ] )
120 return file(pubfile).read().strip()
122 # a sliver can get created, deleted and re-created
123 # the slice having the tag is not sufficient to skip key geneneration
124 def manage_sshkey (plc, sliver):
125 # regardless of whether the tag is there or not, we need to grab the file
126 # if it's lost b/c e.g. the sliver was destroyed we cannot save the tags content
127 ssh_key = generate_sshkey(sliver)
128 old_tag = find_tag (sliver, 'ssh_key')
129 if ssh_key <> old_tag:
130 SetSliverTag(plc, sliver['name'], 'ssh_key', ssh_key)
131 logger.log ("sliverauth: %s: setting ssh_key" % sliver['name'])