Ejemplo n.º 1
0
def requests_session(db, token):
    """
    Create a Requests API for testing, uses the token for auth
    """
    auth_interceptor = Auth(db).get_auth_interceptor(allow_jailed=False)
    user_id, jailed = Auth(db).get_session_for_token(token)
    channel = FakeChannel(user_id=user_id)
    requests_pb2_grpc.add_RequestsServicer_to_server(Requests(db), channel)
    yield requests_pb2_grpc.RequestsStub(channel)
Ejemplo n.º 2
0
def account_session(db, token):
    """
    Create a Account API for testing, uses the token for auth
    """
    auth_interceptor = Auth(db).get_auth_interceptor(allow_jailed=False)
    user_id, jailed = Auth(db).get_session_for_token(token)
    channel = FakeChannel(user_id=user_id)
    account_pb2_grpc.add_AccountServicer_to_server(Account(db), channel)
    yield account_pb2_grpc.AccountStub(channel)
Ejemplo n.º 3
0
def generate_user(db, *_, **kwargs):
    """
    Create a new user, return session token

    The user is detached from any session, and you can access its static attributes, but you can't modify it

    Use this most of the time
    """
    auth = Auth(db)

    with session_scope(db) as session:
        # default args
        username = "******" + random_hex(16)
        user_opts = {
            "username": username,
            "email": f"{username}@dev.couchers.org",
            # password is just 'password'
            # this is hardcoded because the password is slow to hash (so would slow down tests otherwise)
            "hashed_password":
            b"$argon2id$v=19$m=65536,t=2,p=1$4cjGg1bRaZ10k+7XbIDmFg$tZG7JaLrkfyfO7cS233ocq7P8rf3znXR7SAfUt34kJg",
            "name": username.capitalize(),
            "city": "Testing city",
            "verification": 0.5,
            "community_standing": 0.5,
            "birthdate": date(year=2000, month=1, day=1),
            "gender": "N/A",
            "languages": "Testing language 1|Testing language 2",
            "occupation": "Tester",
            "about_me": "I test things",
            "about_place": "My place has a lot of testing paraphenelia",
            "countries_visited": "Testing country",
            "countries_lived": "Wonderland",
            # you need to make sure to update this logic to make sure the user is jailed/not on request
            "accepted_tos": 1,
        }

        for key, value in kwargs.items():
            user_opts[key] = value

        user = User(**user_opts)

        session.add(user)

        # this expires the user, so now it's "dirty"
        session.commit()

        token = auth._create_session("Dummy context", session, user)

        # refresh it, undoes the expiry
        session.refresh(user)
        # allows detaches the user from the session, allowing its use outside this session
        session.expunge(user)

    return user, token
Ejemplo n.º 4
0
def auth_api_session(db):
    """
    Create an Auth API for testing
    """
    channel = FakeChannel()
    auth_pb2_grpc.add_AuthServicer_to_server(Auth(db), channel)
    yield auth_pb2_grpc.AuthStub(channel)
Ejemplo n.º 5
0
def real_jail_session(token):
    """
    Create a Jail service for testing, using TCP sockets, uses the token for auth
    """
    auth_interceptor = Auth().get_auth_interceptor(allow_jailed=True)

    with futures.ThreadPoolExecutor(1) as executor:
        server = grpc.server(executor, interceptors=[auth_interceptor])
        port = server.add_secure_port("localhost:0",
                                      grpc.local_server_credentials())
        servicer = Jail()
        jail_pb2_grpc.add_JailServicer_to_server(servicer, server)
        server.start()

        call_creds = grpc.metadata_call_credentials(
            CookieMetadataPlugin(token))
        comp_creds = grpc.composite_channel_credentials(
            grpc.local_channel_credentials(), call_creds)

        try:
            with grpc.secure_channel(f"localhost:{port}",
                                     comp_creds) as channel:
                yield jail_pb2_grpc.JailStub(channel)
        finally:
            server.stop(None).wait()
Ejemplo n.º 6
0
def auth_api_session():
    """
    Create an Auth API for testing

    This needs to use the real server since it plays around with headers
    """
    with futures.ThreadPoolExecutor(1) as executor:
        server = grpc.server(executor)
        port = server.add_secure_port("localhost:0",
                                      grpc.local_server_credentials())
        auth_pb2_grpc.add_AuthServicer_to_server(Auth(), server)
        server.start()

        try:
            with grpc.secure_channel(
                    f"localhost:{port}",
                    grpc.local_channel_credentials()) as channel:

                class _MetadataKeeperInterceptor(
                        grpc.UnaryUnaryClientInterceptor):
                    def __init__(self):
                        self.latest_headers = {}

                    def intercept_unary_unary(self, continuation,
                                              client_call_details, request):
                        call = continuation(client_call_details, request)
                        self.latest_headers = dict(call.initial_metadata())
                        return call

                metadata_interceptor = _MetadataKeeperInterceptor()
                channel = grpc.intercept_channel(channel, metadata_interceptor)
                yield auth_pb2_grpc.AuthStub(channel), metadata_interceptor
        finally:
            server.stop(None).wait()
Ejemplo n.º 7
0
def api_session(db, token):
    """
    Create an API for testing, uses the token for auth
    """
    user_id, jailed = Auth(db).get_session_for_token(token)
    channel = FakeChannel(user_id=user_id)
    api_pb2_grpc.add_APIServicer_to_server(API(db), channel)
    yield api_pb2_grpc.APIStub(channel)
Ejemplo n.º 8
0
def conversations_session(db, token):
    """
    Create a Conversations API for testing, uses the token for auth
    """
    user_id, jailed = Auth(db).get_session_for_token(token)
    channel = FakeChannel(user_id=user_id)
    conversations_pb2_grpc.add_ConversationsServicer_to_server(
        Conversations(db), channel)
    yield conversations_pb2_grpc.ConversationsStub(channel)
Ejemplo n.º 9
0
def auth_api_session(db_session):
    """
    Create a fresh Auth API for testing

    TODO: investigate if there's a smarter way to stub out these tests?
    """
    auth = Auth(db_session)
    auth_server = grpc.server(futures.ThreadPoolExecutor(1))
    port = auth_server.add_insecure_port("localhost:0")
    auth_pb2_grpc.add_AuthServicer_to_server(auth, auth_server)
    auth_server.start()

    with grpc.insecure_channel(f"localhost:{port}") as channel:
        yield auth_pb2_grpc.AuthStub(channel)

    auth_server.stop(None)
Ejemplo n.º 10
0
def create_main_server(port):
    server = grpc.server(
        futures.ThreadPoolExecutor(SERVER_THREADS),
        interceptors=[
            ErrorSanitizationInterceptor(),
            TracingInterceptor(),
            AuthValidatorInterceptor(),
        ],
    )
    server.add_insecure_port(f"[::]:{port}")

    account_pb2_grpc.add_AccountServicer_to_server(Account(), server)
    admin_pb2_grpc.add_AdminServicer_to_server(Admin(), server)
    api_pb2_grpc.add_APIServicer_to_server(API(), server)
    auth_pb2_grpc.add_AuthServicer_to_server(Auth(), server)
    blocking_pb2_grpc.add_BlockingServicer_to_server(Blocking(), server)
    bugs_pb2_grpc.add_BugsServicer_to_server(Bugs(), server)
    communities_pb2_grpc.add_CommunitiesServicer_to_server(
        Communities(), server)
    conversations_pb2_grpc.add_ConversationsServicer_to_server(
        Conversations(), server)
    discussions_pb2_grpc.add_DiscussionsServicer_to_server(
        Discussions(), server)
    donations_pb2_grpc.add_DonationsServicer_to_server(Donations(), server)
    events_pb2_grpc.add_EventsServicer_to_server(Events(), server)
    gis_pb2_grpc.add_GISServicer_to_server(GIS(), server)
    groups_pb2_grpc.add_GroupsServicer_to_server(Groups(), server)
    jail_pb2_grpc.add_JailServicer_to_server(Jail(), server)
    notifications_pb2_grpc.add_NotificationsServicer_to_server(
        Notifications(), server)
    pages_pb2_grpc.add_PagesServicer_to_server(Pages(), server)
    references_pb2_grpc.add_ReferencesServicer_to_server(References(), server)
    reporting_pb2_grpc.add_ReportingServicer_to_server(Reporting(), server)
    requests_pb2_grpc.add_RequestsServicer_to_server(Requests(), server)
    resources_pb2_grpc.add_ResourcesServicer_to_server(Resources(), server)
    search_pb2_grpc.add_SearchServicer_to_server(Search(), server)
    stripe_pb2_grpc.add_StripeServicer_to_server(Stripe(), server)
    threads_pb2_grpc.add_ThreadsServicer_to_server(Threads(), server)
    return server
Ejemplo n.º 11
0
def real_api_session(db, token):
    """
    Create an API for testing, using TCP sockets, uses the token for auth
    """
    auth_interceptor = Auth(db).get_auth_interceptor(allow_jailed=False)

    with futures.ThreadPoolExecutor(1) as executor:
        server = grpc.server(executor, interceptors=[auth_interceptor])
        port = server.add_secure_port("localhost:0",
                                      grpc.local_server_credentials())
        servicer = API(db)
        api_pb2_grpc.add_APIServicer_to_server(servicer, server)
        server.start()

        call_creds = grpc.access_token_call_credentials(token)
        comp_creds = grpc.composite_channel_credentials(
            grpc.local_channel_credentials(), call_creds)

        try:
            with grpc.secure_channel(f"localhost:{port}",
                                     comp_creds) as channel:
                yield api_pb2_grpc.APIStub(channel)
        finally:
            server.stop(None).wait()
Ejemplo n.º 12
0
def fake_channel(token):
    user_id, jailed = Auth().get_session_for_token(token)
    return FakeChannel(user_id=user_id)
Ejemplo n.º 13
0
            )
            session.add(new_user)


logging.info(f"Adding dummy data")

try:
    add_dummy_data("src/dummy_data.json")
except IntegrityError as e:
    print("Failed to insert dummy data, is it already inserted?")

with session_scope(Session) as session:
    for user in session.query(User).all():
        print(user)

auth = Auth(Session)
auth_server = grpc.server(futures.ThreadPoolExecutor(2))
auth_server.add_insecure_port("[::]:1752")
auth_pb2_grpc.add_AuthServicer_to_server(auth, auth_server)
auth_server.start()

server = grpc.server(futures.ThreadPoolExecutor(2))
server = intercept_server(server, LoggingInterceptor())
server = intercept_server(server, auth.get_auth_interceptor())
server.add_insecure_port("[::]:1751")
servicer = API(Session)
api_pb2_grpc.add_APIServicer_to_server(servicer, server)
server.start()

logging.info(f"Serving on 1751 and 1752 (auth)")
Ejemplo n.º 14
0
with session_scope() as session:
    res = session.execute("SELECT 42;")
    if list(res) != [(42, )]:
        raise Exception("Failed to connect to DB")

logger.info(f"Running DB migrations")

apply_migrations()

if config.config["ADD_DUMMY_DATA"]:
    add_dummy_data()

logger.info(f"Starting")

if config.config["ROLE"] in ["api", "all"]:
    auth = Auth()
    open_server = grpc.server(
        futures.ThreadPoolExecutor(2),
        interceptors=[ErrorSanitizationInterceptor(),
                      LoggingInterceptor()])
    open_server.add_insecure_port("[::]:1752")
    auth_pb2_grpc.add_AuthServicer_to_server(auth, open_server)
    bugs_pb2_grpc.add_BugsServicer_to_server(Bugs(), open_server)
    resources_pb2_grpc.add_ResourcesServicer_to_server(Resources(),
                                                       open_server)
    open_server.start()

    jailed_server = grpc.server(
        futures.ThreadPoolExecutor(2),
        interceptors=[
            ErrorSanitizationInterceptor(),
Ejemplo n.º 15
0
with session_scope(Session) as session:
    res = session.execute("SELECT 42;")
    if list(res) != [(42,)]:
        raise Exception("Failed to connect to DB")

logger.info(f"Running DB migrations")

apply_migrations()

if config.config["ADD_DUMMY_DATA"]:
    add_dummy_data(Session, "src/dummy_data.json")

logger.info(f"Starting")

auth = Auth(Session)
open_server = grpc.server(futures.ThreadPoolExecutor(2), interceptors=[LoggingInterceptor()])
open_server.add_insecure_port("[::]:1752")
auth_pb2_grpc.add_AuthServicer_to_server(auth, open_server)
bugs_pb2_grpc.add_BugsServicer_to_server(Bugs(), open_server)
open_server.start()

jailed_server = grpc.server(
    futures.ThreadPoolExecutor(2), interceptors=[LoggingInterceptor(), auth.get_auth_interceptor(allow_jailed=True)]
)
jailed_server.add_insecure_port("[::]:1754")
jail_pb2_grpc.add_JailServicer_to_server(Jail(Session), jailed_server)
jailed_server.start()

servicer = API(Session)
server = grpc.server(