def _getCandidateCredentials(Framework, remoteUDAUserName, ip): candidates = [] connectedUserName = str(remoteUDAUserName) if connectedUserName == 'root': logger.debug('Connected credential id is irrelevant for this host - connected to root user, no need for sudo') else: allCredIds = [] # Getting all ssh and telnet credentials defined for the ip allCredIds.extend(netutils.getAvailableProtocols(Framework, ClientsConsts.SSH_PROTOCOL_NAME, ip)) allCredIds.extend(netutils.getAvailableProtocols(Framework, ClientsConsts.TELNET_PROTOCOL_NAME, ip)) for credentialId in allCredIds: credential = ProtocolManager.getProtocolById(credentialId) # Get connected protocol details userName = credential.getProtocolAttribute(Protocol.PROTOCOL_ATTRIBUTE_USERNAME, '') sudoCommands = credential.getProtocolAttribute(Protocol.SSH_PROTOCOL_ATTRIBUTE_SUDO_COMMANDS, '') or \ credential.getProtocolAttribute(Protocol.TELNET_PROTOCOL_ATTRIBUTE_SUDO_COMMANDS, '') # Filter out those that don't have sudo defined or do not share same username if connectedUserName == userName and sudoCommands and len(sudoCommands) > 0: candidates.append(credentialId) return candidates
def __getProtocolName(Framework): """ Framework->string Gets protocol name by provided credentialsId in framework """ credentialsId = Framework.getDestinationAttribute('credentialsId') protocol = ProtocolManager.getProtocolById(credentialsId) logger.debug(protocol.getProtocolAttribute(CollectorsConstants.PROTOCOL_ATTRIBUTE_PASSWORD, '')) return protocol.getProtocolName()
def __getProtocolName(Framework): """ Framework->string Gets protocol name by provided credentialsId in framework """ credentialsId = Framework.getDestinationAttribute('credentialsId') protocol = ProtocolManager.getProtocolById(credentialsId) logger.debug( protocol.getProtocolAttribute( CollectorsConstants.PROTOCOL_ATTRIBUTE_PASSWORD, '')) return protocol.getProtocolName()
def getPolicyContent(httpClient, ip, protocolId, protocolName, version): try: protocol = ProtocolManager.getProtocolById(protocolId) protocol_port = protocol.getProtocolAttribute('protocol_port') http_protocol = protocol.getProtocolAttribute('protocol') builder = oam_policy_builder.PolicyBuilder(http_protocol, ip, protocol_port, version, httpClient) return builder.createPolicyDoc() except JException, e: msg = 'URL is not accessable: ' + e.getMessage() errobj = errorobject.createError(errorcodes.CONNECTION_FAILED, [protocolName], msg) logger.reportErrorObject(errobj)
def DiscoveryMain(Framework): OSHVResult = ObjectStateHolderVector() ip = Framework.getDestinationAttribute('ip_address') credential_id = Framework.getDestinationAttribute('credential_id') version = Framework.getDestinationAttribute('version') cmdb_id = Framework.getDestinationAttribute('cmdb_id') protocol = ProtocolManager.getProtocolById(credential_id) host = protocol.getProtocolAttribute('host') port = protocol.getProtocolAttribute('protocol_port') protocolName = ClientsConsts.HTTP_PROTOCOL_NAME if (host and ip != host) or not port: msg = errormessages.makeErrorMessage( protocolName, 'Invalid ip address or missing port in HTTP credential', pattern=errormessages.ERROR_OPERATION_FAILED) errobj = errorobject.createError(errorcodes.OPERATION_FAILED, [protocolName], msg) logger.reportErrorObject(errobj) else: props = Properties() props.setProperty(CollectorsConstants.ATTR_CREDENTIALS_ID, credential_id) props.setProperty('autoAcceptCerts', 'true') props.setProperty('host', ip) try: httpClient = Framework.createClient(props) builder = PolicyBuilder(ip, port, version, httpClient) doc = builder.createPolicyDoc() oamServerOSH = modeling.createOshByCmdbIdString( 'running_software', cmdb_id) policyOSH = modeling.createConfigurationDocumentOSH( 'policy.xml', '', doc, oamServerOSH) linkOSH = modeling.createLinkOSH('composition', oamServerOSH, policyOSH) OSHVResult.add(oamServerOSH) OSHVResult.add(policyOSH) OSHVResult.add(linkOSH) except UnauthorizedException, e: msg = 'Failed to authenticate: ' + e.getMessage() errobj = errorobject.createError( errorcodes.INVALID_USERNAME_PASSWORD, [protocolName], msg) logger.reportErrorObject(errobj) except JException, e: msg = 'URL is not accessable: ' + e.getMessage() errobj = errorobject.createError(errorcodes.CONNECTION_FAILED, [protocolName], msg) logger.reportErrorObject(errobj)
def discoverServers(self): '''Discover application servers @types: -> None''' errobj = None suitableProtocols = None if self.credentialID != None: suitableProtocols = [[ProtocolManager.getProtocolById(self.credentialID), None]] else: j2eePorts = self.Framework.getTriggerCIDataAsList('ports') suitableProtocols = self.getProtocols(j2eePorts) if suitableProtocols.__len__() == 0: errobj = errorobject.createError(errorcodes.PROTOCOL_NOT_DEFINED, [' suitable ' + str(self.getJ2eeServerType())], 'No suitable ' + str(self.getJ2eeServerType()) + ' protocol defined') logger.reportWarningObject(errobj) else: self.discoverServersByCredentials(suitableProtocols)
def DiscoveryMain(Framework): OSHVResult = ObjectStateHolderVector() ip = Framework.getDestinationAttribute("ip_address") ip_domain = Framework.getDestinationAttribute("ip_domain") cmdb_id = Framework.getDestinationAttribute("cmdb_id") protocolName = ClientsConsts.HTTP_PROTOCOL_NAME connectionFailedMsgs = [] protocolIds = findProperProtocolIds( ip, netutils.getAvailableProtocols(Framework, protocolName, ip, ip_domain) or [] ) if not protocolIds: msg = errormessages.makeErrorMessage(protocolName, pattern=errormessages.ERROR_NO_CREDENTIALS) errobj = errorobject.createError(errorcodes.NO_CREDENTIALS_FOR_TRIGGERED_IP, [protocolName], msg) logger.reportErrorObject(errobj) else: for protocolId in protocolIds: protocol = ProtocolManager.getProtocolById(protocolId) port = protocol.getProtocolAttribute("protocol_port") for version in SUPPORTED_OAM_VERSION: props = Properties() props.setProperty(CollectorsConstants.ATTR_CREDENTIALS_ID, protocolId) props.setProperty("autoAcceptCerts", "true") props.setProperty("host", ip) try: httpClient = Framework.createClient(props) httpClient.getAsString( "http://%s:%s/oam/services/rest/%s/ssa/policyadmin/appdomain" % (ip, port, version) ) oamOsh = modeling.createOshByCmdbId("running_software", cmdb_id) oamOsh.setStringAttribute("credentials_id", protocolId) oamOsh.setStringAttribute("version", version) OSHVResult.add(oamOsh) except SocketTimeoutException, e: msg = errormessages.makeErrorMessage(protocolName, pattern=errormessages.ERROR_TIMEOUT) connectionFailedMsgs.append(msg) except JException, e: msg = "URL is not accessable: " + e.getMessage() # logger.debugException(msg) connectionFailedMsgs.append(msg) finally:
def DiscoveryMain(Framework): OSHVResult = ObjectStateHolderVector() ip = Framework.getDestinationAttribute('ip_address') credential_id = Framework.getDestinationAttribute('credential_id') version = Framework.getDestinationAttribute('version') cmdb_id = Framework.getDestinationAttribute('cmdb_id') protocol = ProtocolManager.getProtocolById(credential_id) host = protocol.getProtocolAttribute('host') port = protocol.getProtocolAttribute('protocol_port') protocolName = ClientsConsts.HTTP_PROTOCOL_NAME if (host and ip != host) or not port: msg = errormessages.makeErrorMessage(protocolName, 'Invalid ip address or missing port in HTTP credential', pattern=errormessages.ERROR_OPERATION_FAILED) errobj = errorobject.createError(errorcodes.OPERATION_FAILED, [protocolName], msg) logger.reportErrorObject(errobj) else: props = Properties() props.setProperty(CollectorsConstants.ATTR_CREDENTIALS_ID, credential_id) props.setProperty('autoAcceptCerts', 'true') props.setProperty('host', ip) try: httpClient = Framework.createClient(props) builder = PolicyBuilder(ip, port, version, httpClient) doc = builder.createPolicyDoc() oamServerOSH = modeling.createOshByCmdbIdString('running_software', cmdb_id) policyOSH = modeling.createConfigurationDocumentOSH('policy.xml', '', doc, oamServerOSH) linkOSH = modeling.createLinkOSH('composition', oamServerOSH, policyOSH) OSHVResult.add(oamServerOSH) OSHVResult.add(policyOSH) OSHVResult.add(linkOSH) except UnauthorizedException, e: msg = 'Failed to authenticate: ' + e.getMessage() errobj = errorobject.createError(errorcodes.INVALID_USERNAME_PASSWORD, [protocolName], msg) logger.reportErrorObject(errobj) except JException, e: msg = 'URL is not accessable: ' + e.getMessage() errobj = errorobject.createError(errorcodes.CONNECTION_FAILED, [protocolName], msg) logger.reportErrorObject(errobj)
def _uddiRegistryConnect(credId, ip, Framework): r"""@types: str, str, Framework -> Result @raise java.lang.Exception on connection failure """ protocol = ProtocolManager.getProtocolById(credId) url = protocol.getProtocolAttribute(UDDI_PROTOCOL_ATTRIBUTE_URL) props = java.util.Properties() props.setProperty(UDDI_PROTOCOL_ATTRIBUTE_URL, url) props.setProperty("ip_domain", ip) uddiAgent = None for uddiVersion in (3, 2): try: props.setProperty("uddi_version", str(uddiVersion)) uddiAgent = Framework.getAgent(AgentConstants.UDDI_AGENT, "", credId, props) return Result(True) except MissingSdkJarException, ex: logger.debugException(ex.getMessage()) return Result(False, "UDDI SDK jars are missed. Refer documentation for details") finally:
def DiscoveryMain(Framework): OSHVResult = ObjectStateHolderVector() ip = Framework.getDestinationAttribute('ip_address') ip_domain = Framework.getDestinationAttribute('ip_domain') cmdb_id = Framework.getDestinationAttribute('cmdb_id') protocolName = ClientsConsts.HTTP_PROTOCOL_NAME connectionFailedMsgs = [] protocolIds = findProperProtocolIds(ip, netutils.getAvailableProtocols(Framework, protocolName, ip, ip_domain) or []) if not protocolIds: msg = errormessages.makeErrorMessage(protocolName, pattern=errormessages.ERROR_NO_CREDENTIALS) errobj = errorobject.createError(errorcodes.NO_CREDENTIALS_FOR_TRIGGERED_IP, [protocolName], msg) logger.reportErrorObject(errobj) else: for protocolId in protocolIds: protocol = ProtocolManager.getProtocolById(protocolId) port = protocol.getProtocolAttribute('protocol_port') for version in SUPPORTED_OAM_VERSION: props = Properties() props.setProperty(CollectorsConstants.ATTR_CREDENTIALS_ID, protocolId) props.setProperty('autoAcceptCerts', 'true') props.setProperty('host', ip) try: httpClient = Framework.createClient(props) httpClient.getAsString('http://%s:%s/oam/services/rest/%s/ssa/policyadmin/appdomain' % (ip, port, version)) oamOsh = modeling.createOshByCmdbId('running_software', cmdb_id) oamOsh.setStringAttribute('credentials_id', protocolId) oamOsh.setStringAttribute('version', version) OSHVResult.add(oamOsh) except SocketTimeoutException, e: msg = errormessages.makeErrorMessage(protocolName, pattern=errormessages.ERROR_TIMEOUT) connectionFailedMsgs.append(msg) except JException, e: msg = 'URL is not accessable: ' + e.getMessage() # logger.debugException(msg) connectionFailedMsgs.append(msg) finally:
def _getCandidateCredentials(Framework, remoteUDAUserName, ip): candidates = [] connectedUserName = str(remoteUDAUserName) if connectedUserName == 'root': logger.debug( 'Connected credential id is irrelevant for this host - connected to root user, no need for sudo' ) else: allCredIds = [] # Getting all ssh and telnet credentials defined for the ip allCredIds.extend( netutils.getAvailableProtocols(Framework, ClientsConsts.SSH_PROTOCOL_NAME, ip)) allCredIds.extend( netutils.getAvailableProtocols(Framework, ClientsConsts.TELNET_PROTOCOL_NAME, ip)) for credentialId in allCredIds: credential = ProtocolManager.getProtocolById(credentialId) # Get connected protocol details userName = credential.getProtocolAttribute( Protocol.PROTOCOL_ATTRIBUTE_USERNAME, '') sudoCommands = credential.getProtocolAttribute(Protocol.SSH_PROTOCOL_ATTRIBUTE_SUDO_COMMANDS, '') or \ credential.getProtocolAttribute(Protocol.TELNET_PROTOCOL_ATTRIBUTE_SUDO_COMMANDS, '') # Filter out those that don't have sudo defined or do not share same username if connectedUserName == userName and sudoCommands and len( sudoCommands) > 0: candidates.append(credentialId) return candidates
def discoverServers(self): '''Discover application servers @types: -> None''' errobj = None suitableProtocols = None if self.credentialID != None: suitableProtocols = [[ ProtocolManager.getProtocolById(self.credentialID), None ]] else: j2eePorts = self.Framework.getTriggerCIDataAsList('ports') suitableProtocols = self.getProtocols(j2eePorts) if suitableProtocols.__len__() == 0: errobj = errorobject.createError( errorcodes.PROTOCOL_NOT_DEFINED, [' suitable ' + str(self.getJ2eeServerType())], 'No suitable ' + str(self.getJ2eeServerType()) + ' protocol defined') logger.reportWarningObject(errobj) else: self.discoverServersByCredentials(suitableProtocols)
def findHttpProtocol(Framework, protocolName, protocolIds, ip, connectionFailedMsgs): for protocolId in protocolIds: protocol = ProtocolManager.getProtocolById(protocolId) protocol_port = protocol.getProtocolAttribute('protocol_port') http_protocol = protocol.getProtocolAttribute('protocol') for version in oam_policy_builder.SUPPORTED_OAM_VERSION: props = Properties() props.setProperty(CollectorsConstants.ATTR_CREDENTIALS_ID, protocolId) props.setProperty('autoAcceptCerts', 'true') props.setProperty('host', ip) try: httpClient = Framework.createClient(props) httpClient.getAsString('%s://%s:%s/oam/services/rest/%s/ssa/policyadmin/appdomain' % ( http_protocol, ip, protocol_port, version)) return httpClient, protocolId, version except SocketTimeoutException: msg = errormessages.makeErrorMessage(protocolName, pattern=errormessages.ERROR_TIMEOUT) connectionFailedMsgs.append(msg) except JException, e: msg = 'URL is not accessable: ' + e.getMessage() connectionFailedMsgs.append(msg)
def _uddiRegistryConnect(credId, ip, Framework): r'''@types: str, str, Framework -> Result @raise java.lang.Exception on connection failure ''' protocol = ProtocolManager.getProtocolById(credId) url = protocol.getProtocolAttribute(UDDI_PROTOCOL_ATTRIBUTE_URL) props = java.util.Properties() props.setProperty(UDDI_PROTOCOL_ATTRIBUTE_URL, url) props.setProperty("ip_domain", ip) uddiAgent = None for uddiVersion in (3, 2): try: props.setProperty('uddi_version', str(uddiVersion)) uddiAgent = Framework.getAgent(AgentConstants.UDDI_AGENT, '', credId, props) return Result(True) except MissingSdkJarException, ex: logger.debugException(ex.getMessage()) return Result( False, "UDDI SDK jars are missed. Refer documentation for details") finally:
def isProperProtocol(ip, protocolId): protocol = ProtocolManager.getProtocolById(protocolId) host = protocol.getProtocolAttribute("host") port = protocol.getProtocolAttribute("protocol_port") return port and (not host or ip == host)
def isProperProtocol(ip, protocolId): protocol = ProtocolManager.getProtocolById(protocolId) host = protocol.getProtocolAttribute('host') port = protocol.getProtocolAttribute('protocol_port') return port and (not host or ip == host)