Ejemplo n.º 1
0
    def action(self) -> typing.MutableMapping[str, typing.Any]:
        """
        unmanaged method expect a json POST with this fields:
            * id: List[dict] -> List of dictionary containing ip and mac:
            * token: str -> Valid Actor "master_token" (if invalid, will return an error).
            * secret: Secret for commsUrl for actor
            * port: port of the listener (normally 43910)

        This method will also regenerater the public-private key pair for client, that will be needed for the new ip

        Returns: {
            private_key: str -> Generated private key, PEM
            server_certificate: str -> Generated public key, PEM
        }
        """
        logger.debug('Args: %s,  Params: %s', self._args, self._params)

        try:
            dbService: Service = Service.objects.get(
                token=self._params['token'])
            service: 'services.Service' = dbService.getInstance()
        except Exception:
            return ActorV3Action.actorResult(error='Invalid token')

        # Build the possible ids and ask service if it recognizes any of it
        # If not recognized, will generate anyway the certificate, but will not be saved
        idsList = [x['ip'] for x in self._params['id']
                   ] + [x['mac'] for x in self._params['id']][:10]
        validId: typing.Optional[str] = service.getValidId(idsList)
        ip: str
        try:
            ip = next(x['ip'] for x in self._params['id']
                      if x['ip'] == validId or x['mac'] == validId)
        except StopIteration:
            ip = self._params['id'][0][
                'ip']  # Get first IP if no valid ip found

        # Generates a certificate and send it to client.
        privateKey, certificate, password = certs.selfSignedCert(ip)
        cert: typing.Dict[str, str] = {
            'private_key': privateKey,
            'server_certificate': certificate,
            'password': password
        }
        if validId:
            # Notify service of it "just start" action
            service.notifyInitialization(validId)

            # Store certificate, secret & port with service if validId
            service.storeIdInfo(
                validId, {
                    'cert': certificate,
                    'secret': self._params['secret'],
                    'port': int(self._params['port'])
                })

        return ActorV3Action.actorResult(cert)
Ejemplo n.º 2
0
    def action(self) -> typing.MutableMapping[str, typing.Any]:
        """
        BaseReady method expect a json POST with this fields:
            * token: str -> Valid Actor "own_token" (if invalid, will return an error).
              Currently it is the same as user service uuid, but this could change
            * secret: Secret for commsUrl for actor
            * ip: ip accesible by uds
            * port: port of the listener (normally 43910)

        This method will also regenerater the public-private key pair for client, that will be needed for the new ip

        Returns: {
            private_key: str -> Generated private key, PEM
            server_certificate: str -> Generated public key, PEM
        }
        """
        logger.debug('Args: %s,  Params: %s', self._args, self._params)
        userService = self.getUserService()
        # Stores known IP and notifies it to deployment
        userService.logIP(self._params['ip'])
        userServiceInstance = userService.getInstance()
        userServiceInstance.setIp(self._params['ip'])
        userService.updateData(userServiceInstance)

        # Store communications url also
        ActorV3Action.setCommsUrl(userService, self._params['ip'],
                                  int(self._params['port']),
                                  self._params['secret'])

        if userService.os_state != State.USABLE:
            userService.setOsState(State.USABLE)
            # Notify osManager or readyness if has os manager
            osManager = userService.getOsManagerInstance()

            if osManager:
                osManager.toReady(userService)
                userServiceManager().notifyReadyFromOsManager(userService, '')

        # Generates a certificate and send it to client.
        privateKey, cert, password = certs.selfSignedCert(self._params['ip'])
        # Store certificate with userService
        userService.setProperty('cert', cert)
        userService.setProperty('priv', privateKey)
        userService.setProperty('priv_passwd', password)

        return ActorV3Action.actorResult({
            'private_key': privateKey,
            'server_certificate': cert,
            'password': password
        })