Ejemplo n.º 1
0
def adminInterface(controller, hab, adminHttpPort=5623, adminTcpPort=5624):

    rotateHandler = agenting.RotateHandler(hab=hab)
    issueHandler = agenting.IssueCredentialHandler(hab=hab)
    handlers = [rotateHandler, issueHandler]

    exchanger = exchanging.Exchanger(hab=hab,
                                     controller=controller,
                                     handlers=handlers)

    server = tcpServing.Server(host="", port=adminTcpPort)
    tcpServerDoer = tcpServing.ServerDoer(server=server)
    directant = directing.Directant(hab=hab, server=server)

    app = falcon.App()
    exnServer = httping.AgentExnServer(exc=exchanger, app=app)
    httpKelServer = httping.AgentKelServer(hab=hab, app=app)

    server = http.Server(port=adminHttpPort, app=exnServer.app)
    httpServerDoer = http.ServerDoer(server=server)

    doers = [
        exnServer, exchanger, tcpServerDoer, directant, httpKelServer,
        httpServerDoer
    ]

    return doers
Ejemplo n.º 2
0
def runAgent(controller,
             name="agent",
             httpPort=5620,
             tcp=5621,
             adminHttpPort=5623,
             adminTcpPort=5624,
             expire=0.0):
    """
    Setup and run one agent
    """
    logger = help.ogler.getLogger()

    ks = keeping.Keeper(name=name,
                        temp=False)  # not opened by default, doer opens
    ksDoer = keeping.KeeperDoer(
        keeper=ks)  # doer do reopens if not opened and closes
    db = basing.Baser(name=name, temp=False,
                      reload=True)  # not opened by default, doer opens
    dbDoer = basing.BaserDoer(
        baser=db)  # doer do reopens if not opened and closes

    # setup habitat
    hab = habbing.Habitat(name=name, ks=ks, db=db, temp=False, create=False)
    habDoer = habbing.HabitatDoer(habitat=hab)  # setup doer

    # kvy = eventing.Kevery(db=hab.db, local=False)
    # setup doers
    server = tcpServing.Server(host="", port=tcp)
    tcpServerDoer = tcpServing.ServerDoer(server=server)
    directant = directing.Directant(hab=hab, server=server)

    wallet = walleting.Wallet(hab=hab, name=name)

    jsonSchema = scheming.JSONSchema(resolver=scheming.jsonSchemaCache)
    issueHandler = handling.IssueHandler(wallet=wallet, typ=jsonSchema)
    requestHandler = handling.RequestHandler(wallet=wallet, typ=jsonSchema)
    exchanger = exchanging.Exchanger(hab=hab,
                                     handlers=[issueHandler, requestHandler])

    app = falcon.App()
    exnServer = httping.AgentExnServer(exc=exchanger, app=app)
    httpKelServer = httping.AgentKelServer(hab=hab, app=app)

    server = http.Server(port=httpPort, app=exnServer.app)
    httpServerDoer = http.ServerDoer(server=server)

    doers = [
        ksDoer, dbDoer, habDoer, exchanger, directant, tcpServerDoer,
        exnServer, httpServerDoer, httpKelServer
    ]
    doers.extend(adminInterface(controller, hab, adminHttpPort, adminTcpPort))

    try:
        tock = 0.03125
        doist = doing.Doist(limit=expire, tock=tock, real=True)
        doist.do(doers=doers)
    except kering.ConfigurationError:
        print(f"prefix for {name} does not exist, incept must be run first", )
Ejemplo n.º 3
0
def test_directing_basic():
    """
    Test directing
    """
    help.ogler.resetLevel(level=logging.INFO)

    # set of secrets  (seeds for private keys)
    bobSecrets = [
        'ArwXoACJgOleVZ2PY7kXn7rA0II0mHYDhc6WrBH8fDAc',
        'A6zz7M08-HQSFq92sJ8KJOT2cZ47x7pXFQLPB0pckB3Q',
        'AcwFTk-wgk3ZT2buPRIbK-zxgPx-TKbaegQvPEivN90Y',
        'Alntkt3u6dDgiQxTATr01dy8M72uuaZEf9eTdM-70Gk8',
        'A1-QxDkso9-MR1A8rZz_Naw6fgaAtayda8hrbkRVVu1E',
        'AKuYMe09COczwf2nIoD5AE119n7GLFOVFlNLxZcKuswc',
        'AxFfJTcSuEE11FINfXMqWttkZGnUZ8KaREhrnyAXTsjw',
        'ALq-w1UKkdrppwZzGTtz4PWYEeWm0-sDHzOv5sq96xJY'
    ]

    bobSecrecies = []
    for secret in bobSecrets:  # convert secrets to secrecies
        bobSecrecies.append([secret])

    #  create bob signers
    bobSigners = [coring.Signer(qb64=secret) for secret in bobSecrets]
    assert [signer.qb64 for signer in bobSigners] == bobSecrets

    # bob inception transferable (nxt digest not empty)
    bobSerder = eventing.incept(keys=[bobSigners[0].verfer.qb64],
                                nxt=coring.Nexter(keys=[bobSigners[1].verfer.qb64]).qb64,
                                code=coring.MtrDex.Blake3_256)

    bob = bobSerder.ked["i"]
    assert bob == 'EQf1hzB6s5saaQPdDAsEzSMEFoQx_WLsq93bjPu5wuqA'

    # set of secrets (seeds for private keys)
    eveSecrets = ['AgjD4nRlycmM5cPcAkfOATAp8wVldRsnc9f1tiwctXlw',
                  'AKUotEE0eAheKdDJh9QvNmSEmO_bjIav8V_GmctGpuCQ',
                  'AK-nVhMMJciMPvmF5VZE_9H-nhrgng9aJWf7_UHPtRNM',
                  'AT2cx-P5YUjIw_SLCHQ0pqoBWGk9s4N1brD-4pD_ANbs',
                  'Ap5waegfnuP6ezC18w7jQiPyQwYYsp9Yv9rYMlKAYL8k',
                  'Aqlc_FWWrxpxCo7R12uIz_Y2pHUH2prHx1kjghPa8jT8',
                  'AagumsL8FeGES7tYcnr_5oN6qcwJzZfLKxoniKUpG4qc',
                  'ADW3o9m3udwEf0aoOdZLLJdf1aylokP0lwwI_M2J9h0s']

    eveSecrecies = []
    for secret in eveSecrets:  # convert secrets to secrecies
        eveSecrecies.append([secret])

    #  create eve signers
    eveSigners = [coring.Signer(qb64=secret) for secret in eveSecrets]
    assert [signer.qb64 for signer in eveSigners] == eveSecrets

    # eve inception transferable (nxt digest not empty)
    eveSerder = eventing.incept(keys=[eveSigners[0].verfer.qb64],
                                nxt=coring.Nexter(keys=[eveSigners[1].verfer.qb64]).qb64,
                                code=coring.MtrDex.Blake3_256)

    eve = eveSerder.ked["i"]
    assert eve == 'ED9EB3sA5u2vCPOEmX3d7bEyHiSh7Xi8fjew2KMl3FQM'

    with basing.openDB(name="eve") as eveDB, keeping.openKS(name="eve") as eveKS, \
            basing.openDB(name="bob") as bobDB, keeping.openKS(name="bob") as bobKS:

        limit = 0.125
        tock = 0.03125
        doist = doing.Doist(limit=limit, tock=tock)

        bobPort = 5620  # bob's TCP listening port for server
        evePort = 5621  # eve's TCP listneing port for server
        eveKevers = dict()

        # setup bob
        bobHab = habbing.Habitat(ks=bobKS, db=bobDB, secrecies=bobSecrecies,
                                 temp=True)
        assert bobHab.ks == bobKS
        assert bobHab.db == bobDB
        assert bobHab.iserder.dig == bobSerder.dig
        assert bobHab.pre == bob

        bobClient = clienting.Client(tymth=doist.tymen(), host='127.0.0.1', port=evePort)
        bobClientDoer = clienting.ClientDoer(tymth=doist.tymen(), client=bobClient)

        bobDirector = directing.Director(hab=bobHab, client=bobClient)
        assert bobDirector.hab == bobHab
        assert bobDirector.client == bobClient
        assert id(bobDirector.hab.kvy.kevers) == id(bobHab.kevers)
        assert bobDirector.hab.kvy.db == bobDB

        bobReactor = directing.Reactor(hab=bobHab, client=bobClient)
        assert bobReactor.hab == bobHab
        assert bobReactor.client == bobClient
        assert id(bobReactor.hab.kvy.kevers) == id(bobHab.kevers)
        assert bobReactor.hab.kvy.db == bobDB
        assert id(bobReactor.parser.ims) == id(bobReactor.client.rxbs)
        assert id(bobReactor.client.rxbs) == id(bobDirector.client.rxbs)

        bobServer = serving.Server(host="", port=bobPort)
        bobServerDoer = serving.ServerDoer(server=bobServer)

        bobDirectant = directing.Directant(hab=bobHab, server=bobServer)
        assert bobDirectant.hab == bobHab
        assert bobDirectant.server == bobServer
        # Bob's Reactants created on demand

        # setup eve
        eveHab = habbing.Habitat(ks=eveKS, db=eveDB, secrecies=eveSecrecies,
                                 temp=True)
        assert eveHab.ks == eveKS
        assert eveHab.db == eveDB
        assert eveHab.iserder.dig == eveSerder.dig
        assert eveHab.pre == eve

        eveClient = clienting.Client(tymth=doist.tymen(), host='127.0.0.1', port=bobPort)
        eveClientDoer = clienting.ClientDoer(tymth=doist.tymen(), client=eveClient)

        eveDirector = directing.Director(hab=eveHab, client=eveClient)
        assert eveDirector.hab == eveHab
        assert eveDirector.client == eveClient
        assert id(eveDirector.hab.kvy.kevers) == id(eveHab.kevers)
        assert eveDirector.hab.kvy.db == eveDB

        eveReactor = directing.Reactor(hab=eveHab, client=eveClient)
        assert eveReactor.hab == eveHab
        assert eveReactor.client == eveClient
        assert id(eveReactor.hab.kvy.kevers) == id(eveHab.kevers)
        assert eveReactor.hab.kvy.db == eveDB
        assert id(eveReactor.parser.ims) == id(eveReactor.client.rxbs)
        assert id(eveReactor.client.rxbs) == id(eveDirector.client.rxbs)

        eveServer = serving.Server(host="", port=evePort)
        eveServerDoer = serving.ServerDoer(server=eveServer)

        eveDirectant = directing.Directant(hab=eveHab, server=eveServer)
        assert eveDirectant.hab == eveHab
        assert eveDirectant.server == eveServer
        # Eve's Reactants created on demand

        bobMsgTx = b"Hi Eve I am  Bob"
        bobDirector.client.tx(bobMsgTx)

        eveMsgTx = b"Hi Bob its me Eve"
        eveDirector.client.tx(eveMsgTx)

        doers = [bobClientDoer, bobDirector, bobReactor, bobServerDoer, bobDirectant,
                 eveClientDoer, eveDirector, eveReactor, eveServerDoer, eveDirectant]
        doist.do(doers=doers)
        assert doist.tyme == limit

        assert bobClient.opened == False
        assert bobServer.opened == False
        assert eveClient.opened == False
        assert eveServer.opened == False

        assert not bobClient.txbs
        ca, ix = list(eveServer.ixes.items())[0]
        eveMsgRx = bytes(ix.rxbs)  # ColdStart Error fluhes buffer
        assert eveMsgRx == b''
        # assert eveMsgRx == bobMsgTx

        assert not eveClient.txbs
        ca, ix = list(bobServer.ixes.items())[0]
        bobMsgRx = bytes(ix.rxbs)  # ColdStart Error fluhes buffer
        assert bobMsgRx == b''
        # assert bobMsgRx == eveMsgTx

    assert not os.path.exists(eveDB.path)
    assert not os.path.exists(bobDB.path)

    help.ogler.resetLevel(level=help.ogler.level)
    """End Test"""
Ejemplo n.º 4
0
def setupController(secrets, witnessPort=5631, localPort=5629, indirect=False):
    """
    Setup and return doers list to run controller
    """
    secrecies = []
    for secret in secrets:  # convert secrets to secrecies
        secrecies.append([secret])

    # setup habitat
    hab = habbing.Habitat(name="han", secrecies=secrecies, temp=True)
    logger.info(
        "\nDirect Mode demo of %s:\nNamed %s listening on TCP port %s, witness on TCP Port %s.\n\n",
        hab.pre, hab.name, localPort, witnessPort)
    wallet = walleting.Wallet(hab=hab, name="han")

    # setup doers
    ksDoer = keeping.KeeperDoer(
        keeper=hab.ks)  # doer do reopens if not opened and closes
    dbDoer = basing.BaserDoer(
        baser=hab.db)  # doer do reopens if not opened and closes
    pdbDoer = basing.BaserDoer(
        baser=wallet.db)  # doer do reopens if not opened and closes

    path = os.path.dirname(__file__)
    path = os.path.join(path, 'logs')

    wl = wiring.WireLog(samed=True,
                        filed=True,
                        name="han",
                        prefix='demo',
                        reopen=True,
                        headDirPath=path)
    wireDoer = wiring.WireLogDoer(wl=wl)

    jsonSchema = scheming.JSONSchema(resolver=scheming.jsonSchemaCache)

    issueHandler = handling.IssueHandler(wallet=wallet, typ=jsonSchema)
    requestHandler = handling.RequestHandler(wallet=wallet, typ=jsonSchema)

    witnessClient = clienting.Client(host='127.0.0.1', port=witnessPort, wl=wl)
    witnessClientDoer = clienting.ClientDoer(client=witnessClient)

    excDoer = exchanging.Exchanger(hab=hab,
                                   handlers=[issueHandler, requestHandler])
    director = HanDirector(hab=hab,
                           client=witnessClient,
                           exchanger=excDoer,
                           tock=0.125,
                           wallet=wallet)

    reactor = directing.Reactor(hab=hab,
                                client=witnessClient,
                                indirect=indirect)
    server = serving.Server(host="", port=localPort)
    serverDoer = serving.ServerDoer(server=server)
    directant = directing.Directant(hab=hab, server=server, exchanger=excDoer)

    return [
        ksDoer, dbDoer, pdbDoer, excDoer, wireDoer, witnessClientDoer,
        director, reactor, serverDoer, directant
    ]
Ejemplo n.º 5
0
def test_indirect_mode_sam_cam_wit_demo():
    """ Test indirect mode, bob and eve with witness """

    help.ogler.resetLevel(level=logging.DEBUG)
    # set of secrets (seeds for private keys)
    camSecrets = [
        'AgjD4nRlycmM5cPcAkfOATAp8wVldRsnc9f1tiwctXlw',
        'AKUotEE0eAheKdDJh9QvNmSEmO_bjIav8V_GmctGpuCQ',
        'AK-nVhMMJciMPvmF5VZE_9H-nhrgng9aJWf7_UHPtRNM',
        'AT2cx-P5YUjIw_SLCHQ0pqoBWGk9s4N1brD-4pD_ANbs',
        'Ap5waegfnuP6ezC18w7jQiPyQwYYsp9Yv9rYMlKAYL8k',
        'Aqlc_FWWrxpxCo7R12uIz_Y2pHUH2prHx1kjghPa8jT8',
        'AagumsL8FeGES7tYcnr_5oN6qcwJzZfLKxoniKUpG4qc',
        'ADW3o9m3udwEf0aoOdZLLJdf1aylokP0lwwI_M2J9h0s'
    ]

    camSecrecies = []
    for secret in camSecrets:  # convert secrets to secrecies
        camSecrecies.append([secret])

    #  create cam signers
    camSigners = [coring.Signer(qb64=secret) for secret in camSecrets]
    assert [signer.qb64 for signer in camSigners] == camSecrets

    # set of secrets  (seeds for private keys)
    samSecrets = [
        'ArwXoACJgOleVZ2PY7kXn7rA0II0mHYDhc6WrBH8fDAc',
        'A6zz7M08-HQSFq92sJ8KJOT2cZ47x7pXFQLPB0pckB3Q',
        'AcwFTk-wgk3ZT2buPRIbK-zxgPx-TKbaegQvPEivN90Y',
        'Alntkt3u6dDgiQxTATr01dy8M72uuaZEf9eTdM-70Gk8',
        'A1-QxDkso9-MR1A8rZz_Naw6fgaAtayda8hrbkRVVu1E',
        'AKuYMe09COczwf2nIoD5AE119n7GLFOVFlNLxZcKuswc',
        'AxFfJTcSuEE11FINfXMqWttkZGnUZ8KaREhrnyAXTsjw',
        'ALq-w1UKkdrppwZzGTtz4PWYEeWm0-sDHzOv5sq96xJY'
    ]

    samSecrecies = []
    for secret in samSecrets:  # convert secrets to secrecies
        samSecrecies.append([secret])

    #  create sam signers
    samSigners = [coring.Signer(qb64=secret) for secret in samSecrets]
    assert [signer.qb64 for signer in samSigners] == samSecrets

    with basing.openDB(name="cam") as camDB, keeping.openKS(name="cam") as camKS, \
         basing.openDB(name="sam") as samDB, keeping.openKS(name="sam") as samKS, \
         basing.openDB(name="wit") as witDB, keeping.openKS(name="wit") as witKS:

        samPort = 5620  # sam's TCP listening port for server
        witPort = 5621  # wit' TCP listneing port for server

        # setup the witness
        witHab = habbing.Habitat(name='Wit',
                                 ks=witKS,
                                 db=witDB,
                                 isith=1,
                                 icount=1,
                                 temp=True,
                                 transferable=False)
        wit = witHab.pre
        assert witHab.ks == witKS
        assert witHab.db == witDB
        witServer = serving.Server(host="", port=witPort)
        witServerDoer = serving.ServerDoer(server=witServer)
        witDirectant = directing.Directant(hab=witHab, server=witServer)

        witDoers = [witServerDoer, witDirectant]

        # setup cam
        # cam inception transferable (nxt digest not empty)
        camSerder = eventing.incept(
            keys=[camSigners[0].verfer.qb64],
            nxt=coring.Nexter(keys=[camSigners[1].verfer.qb64]).qb64,
            code=coring.MtrDex.Blake3_256)

        cam = camSerder.ked["i"]
        assert cam == 'ED9EB3sA5u2vCPOEmX3d7bEyHiSh7Xi8fjew2KMl3FQM'

        # sam inception transferable (nxt digest not empty)
        samSerder = eventing.incept(
            keys=[samSigners[0].verfer.qb64],
            wits=[wit],
            nxt=coring.Nexter(keys=[samSigners[1].verfer.qb64]).qb64,
            code=coring.MtrDex.Blake3_256)

        sam = samSerder.ked["i"]
        assert sam == 'EhnaYUqhHoo8kZaXJuTDCZ-h5ZDx3st4NemgqSITWp48'

        samHab = habbing.Habitat(name='Sam',
                                 ks=samKS,
                                 db=samDB,
                                 wits=[wit],
                                 secrecies=samSecrecies,
                                 temp=True)
        assert samHab.ks == samKS
        assert samHab.db == samDB
        assert samHab.iserder.dig == samSerder.dig
        assert samHab.pre == sam

        samClient = clienting.Client(host='127.0.0.1', port=witPort)
        samClientDoer = clienting.ClientDoer(client=samClient)

        samDirector = demoing.SamDirector(hab=samHab,
                                          client=samClient,
                                          tock=0.125)
        assert samDirector.hab == samHab
        assert samDirector.client == samClient
        assert id(samDirector.hab.kvy.kevers) == id(samHab.kevers)
        assert samDirector.hab.kvy.db == samDB
        assert samDirector.tock == 0.125

        samReactor = directing.Reactor(hab=samHab, client=samClient)
        assert samReactor.hab == samHab
        assert samReactor.client == samClient
        assert id(samReactor.hab.kvy.kevers) == id(samHab.kevers)
        assert samReactor.hab.kvy.db == samDB
        assert samReactor.hab.psr.ims == samReactor.client.rxbs

        samServer = serving.Server(host="", port=samPort)
        samServerDoer = serving.ServerDoer(server=samServer)

        samDirectant = directing.Directant(hab=samHab, server=samServer)
        assert samDirectant.hab == samHab
        assert samDirectant.server == samServer
        # Sam's Reactants created on demand

        samDoers = [
            samClientDoer, samDirector, samReactor, samServerDoer, samDirectant
        ]

        # setup cam
        camHab = habbing.Habitat(name='Cam',
                                 ks=camKS,
                                 db=camDB,
                                 secrecies=camSecrecies,
                                 temp=True)

        assert camHab.ks == camKS
        assert camHab.db == camDB
        assert camHab.iserder.dig == camSerder.dig
        assert camHab.pre == cam

        camClient = clienting.Client(host='127.0.0.1', port=witPort)
        camClientDoer = clienting.ClientDoer(client=camClient)

        camDirector = demoing.CamDirector(hab=camHab,
                                          remotePre=sam,
                                          client=camClient,
                                          tock=0.125)
        assert camDirector.hab == camHab
        assert camDirector.client == camClient
        assert id(camDirector.hab.kvy.kevers) == id(camHab.kevers)
        assert camDirector.hab.kvy.db == camDB
        assert camDirector.tock == 0.125

        camReactor = directing.Reactor(hab=camHab,
                                       client=camClient,
                                       indirect=True)
        assert camReactor.hab == camHab
        assert camReactor.client == camClient
        assert id(camReactor.hab.kvy.kevers) == id(camHab.kevers)
        assert camReactor.hab.kvy.db == camDB
        assert camReactor.hab.psr.ims == camReactor.client.rxbs

        camDoers = [camClientDoer, camDirector, camReactor]

        # Manually stage SamDoers and wit doers and then camDoers

        tock = 0.03125
        doist = doing.Doist(doers=samDoers + witDoers, tock=tock)

        # manually prep doers
        doist.done = False
        doist.enter()  # injects doist.tymth() dependency to all doers
        assert len(doist.deeds) == len(samDoers + witDoers)
        while doist.tyme < doist.tock * 16:
            doist.recur()  # iterate 16 times
            time.sleep(doist.tock)

        assert len(doist.deeds) == len(samDoers + witDoers)
        assert not doist.done
        assert samHab.pre in samHab.kevers

        # now start up cam doers
        doist.extend(camDoers)
        assert len(doist.deeds) == len(samDoers + witDoers + camDoers)

        while doist.tyme < doist.tock * 32:
            doist.recur()  # iterate 16 times
            time.sleep(doist.tock)

        assert len(doist.deeds) == len(samDoers + witDoers + camDoers) - 1
        # camDirector completes
        assert camDirector.done
        assert not doist.done
        assert camHab.pre in camHab.kevers
        assert samHab.pre in camHab.kevers

        #  verify final event states

        doist.exit()
        assert samClient.opened is False
        assert samServer.opened is False
        assert camClient.opened is False

    assert not os.path.exists(camDB.path)
    assert not os.path.exists(samDB.path)

    help.ogler.resetLevel(level=help.ogler.level)
    """End Test"""