Class | MCollective::Security::Base |
In: |
lib/mcollective/security/base.rb
|
Parent: | Object |
This is a base class the other security modules should inherit from it handles statistics and validation of messages that should in most cases apply to all security models.
To create your own security plugin you should provide a plugin that inherits from this and provides the following methods:
decodemsg - Decodes a message that was received from the middleware encodereply - Encodes a reply message to a previous request message encoderequest - Encodes a new request message validrequest? - Validates a request received from the middleware
Optionally if you are identifying users by some other means like certificate name you can provide your own callerid method that can provide the rest of the system with an id, and you would see this id being usable in SimpleRPC authorization methods
The @initiated_by variable will be set to either :client or :node depending on who is using this plugin. This is to help security providers that operate in an asymetric mode like public/private key based systems.
Specifics of each of these are a bit fluid and the interfaces for this is not set in stone yet, specifically the encode methods will be provided with a helper that takes care of encoding the core requirements. The best place to see how security works is by looking at the provided MCollective::Security::PSK plugin.
initiated_by | [RW] | |
stats | [R] |
Register plugins that inherits base
# File lib/mcollective/security/base.rb, line 32 32: def self.inherited(klass) 33: PluginManager << {:type => "security_plugin", :class => klass.to_s} 34: end
Initializes configuration and logging as well as prepare a zero‘d hash of stats various security methods and filter validators should increment stats, see MCollective::Security::Psk for a sample
# File lib/mcollective/security/base.rb, line 38 38: def initialize 39: @config = Config.instance 40: @log = Log 41: @stats = PluginManager["global_stats"] 42: end
Returns a unique id for the caller, by default we just use the unix user id, security plugins can provide their own means of doing ids.
# File lib/mcollective/security/base.rb, line 212 212: def callerid 213: "uid=#{Process.uid}" 214: end
# File lib/mcollective/security/base.rb, line 160 160: def create_reply(reqid, agent, body) 161: Log.debug("Encoded a message for request #{reqid}") 162: 163: {:senderid => @config.identity, 164: :requestid => reqid, 165: :senderagent => agent, 166: :msgtime => Time.now.utc.to_i, 167: :body => body} 168: end
# File lib/mcollective/security/base.rb, line 170 170: def create_request(reqid, filter, msg, initiated_by, target_agent, target_collective, ttl=60) 171: Log.debug("Encoding a request for agent '#{target_agent}' in collective #{target_collective} with request id #{reqid}") 172: 173: {:body => msg, 174: :senderid => @config.identity, 175: :requestid => reqid, 176: :filter => filter, 177: :collective => target_collective, 178: :agent => target_agent, 179: :callerid => callerid, 180: :ttl => ttl, 181: :msgtime => Time.now.utc.to_i} 182: end
Give a MC::Message instance and a message id this will figure out if you the incoming message id matches the one the Message object is expecting and raise if its not
Mostly used by security plugins to figure out if they should do the hard work of decrypting etc messages that would only later on be ignored
# File lib/mcollective/security/base.rb, line 189 189: def should_process_msg?(msg, msgid) 190: if msg.expected_msgid 191: unless msg.expected_msgid == msgid 192: msgtext = "Got a message with id %s but was expecting %s, ignoring message" % [msgid, msg.expected_msgid] 193: Log.debug msgtext 194: raise MsgDoesNotMatchRequestID, msgtext 195: end 196: end 197: 198: true 199: end
Validates a callerid. We do not want to allow things like \ and / in callerids since other plugins make assumptions that these are safe strings.
callerids are generally in the form uid=123 or cert=foo etc so we do that here but security plugins could override this for some complex uses
# File lib/mcollective/security/base.rb, line 206 206: def valid_callerid?(id) 207: !!id.match(/^[\w]+=[\w\.\-]+$/) 208: end
Takes a Hash with a filter in it and validates it against host information.
At present this supports filter matches against the following criteria:
the file configured with classesfile
TODO: Support REGEX and/or multiple filter keys to be AND‘d
# File lib/mcollective/security/base.rb, line 55 55: def validate_filter?(filter) 56: failed = 0 57: passed = 0 58: 59: passed = 1 if Util.empty_filter?(filter) 60: 61: filter.keys.each do |key| 62: case key 63: when /puppet_class|cf_class/ 64: filter[key].each do |f| 65: Log.debug("Checking for class #{f}") 66: if Util.has_cf_class?(f) then 67: Log.debug("Passing based on configuration management class #{f}") 68: passed += 1 69: else 70: Log.debug("Failing based on configuration management class #{f}") 71: failed += 1 72: end 73: end 74: 75: when "compound" 76: filter[key].each do |compound| 77: result = [] 78: 79: compound.each do |expression| 80: case expression.keys.first 81: when "statement" 82: result << Util.eval_compound_statement(expression).to_s 83: when "and" 84: result << "&&" 85: when "or" 86: result << "||" 87: when "(" 88: result << "(" 89: when ")" 90: result << ")" 91: when "not" 92: result << "!" 93: end 94: end 95: 96: result = eval(result.join(" ")) 97: 98: if result 99: Log.debug("Passing based on class and fact composition") 100: passed +=1 101: else 102: Log.debug("Failing based on class and fact composition") 103: failed +=1 104: end 105: end 106: 107: when "agent" 108: filter[key].each do |f| 109: if Util.has_agent?(f) || f == "mcollective" 110: Log.debug("Passing based on agent #{f}") 111: passed += 1 112: else 113: Log.debug("Failing based on agent #{f}") 114: failed += 1 115: end 116: end 117: 118: when "fact" 119: filter[key].each do |f| 120: if Util.has_fact?(f[:fact], f[:value], f[:operator]) 121: Log.debug("Passing based on fact #{f[:fact]} #{f[:operator]} #{f[:value]}") 122: passed += 1 123: else 124: Log.debug("Failing based on fact #{f[:fact]} #{f[:operator]} #{f[:value]}") 125: failed += 1 126: end 127: end 128: 129: when "identity" 130: unless filter[key].empty? 131: # Identity filters should not be 'and' but 'or' as each node can only have one identity 132: matched = filter[key].select{|f| Util.has_identity?(f)}.size 133: 134: if matched == 1 135: Log.debug("Passing based on identity") 136: passed += 1 137: else 138: Log.debug("Failed based on identity") 139: failed += 1 140: end 141: end 142: end 143: end 144: 145: if failed == 0 && passed > 0 146: Log.debug("Message passed the filter checks") 147: 148: @stats.passed 149: 150: return true 151: else 152: Log.debug("Message failed the filter checks") 153: 154: @stats.filtered 155: 156: return false 157: end 158: end