示例#1
0
    def handle_request(s_src, thread):
        s_dst = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s_dst.connect(ip_to_tuple(dst))

        sockets = [s_src, s_dst]

        try:
            while thread.running:
                s_read, _, _ = select.select(sockets, [], [])

                for s in s_read:
                    data = s.recv(BUFFER_SIZE)
                    if data in [b"", "", None]:
                        return

                    if s == s_src:
                        forward, response = data, None
                        if handler:
                            forward, response = handler(data)
                        if forward is not None:
                            s_dst.sendall(forward)
                        elif response is not None:
                            s_src.sendall(response)
                            return
                    elif s == s_dst:
                        s_src.sendall(data)
        finally:
            run_safe(s_src.close)
            run_safe(s_dst.close)
示例#2
0
def create_kinesis_stream(stream_name, shards=1, env=None, delete=False):
    env = get_environment(env)
    stream = KinesisStream(id=stream_name, num_shards=shards)
    conn = connect_to_service("kinesis", env=env)
    stream.connect(conn)
    if delete:
        run_safe(lambda: stream.destroy(), print_error=False)
    stream.create()
    # Note: Returning the stream without awaiting its creation (via wait_for()) to avoid API call timeouts/retries.
    return stream
示例#3
0
def get_template_body(req_data):
    body = req_data.get("TemplateBody")
    if body:
        return body
    url = req_data.get("TemplateURL")
    if url:
        response = run_safe(lambda: safe_requests.get(url, verify=False))
        # check error codes, and code 301 - fixes https://github.com/localstack/localstack/issues/1884
        status_code = 0 if response is None else response.status_code
        if response is None or status_code == 301 or status_code >= 400:
            # check if this is an S3 URL, then get the file directly from there
            url = convert_s3_to_local_url(url)
            if is_local_service_url(url):
                parsed_path = urlparse(url).path.lstrip("/")
                parts = parsed_path.partition("/")
                client = aws_stack.connect_to_service("s3")
                LOG.debug(
                    "Download CloudFormation template content from local S3: %s - %s",
                    parts[0],
                    parts[2],
                )
                result = client.get_object(Bucket=parts[0], Key=parts[2])
                body = to_str(result["Body"].read())
                return body
            raise Exception(
                "Unable to fetch template body (code %s) from URL %s" %
                (status_code, url))
        return response.content
    raise Exception("Unable to get template body from input: %s" % req_data)
示例#4
0
def install_dynamodb_local():
    if not os.path.exists(INSTALL_PATH_DDB_JAR):
        log_install_msg("DynamoDB")
        # download and extract archive
        tmp_archive = os.path.join(tempfile.gettempdir(), "localstack.ddb.zip")
        download_and_extract_with_retry(DYNAMODB_JAR_URL, tmp_archive,
                                        INSTALL_DIR_DDB)

    # fix logging configuration for DynamoDBLocal
    log4j2_config = """<Configuration status="WARN">
      <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
          <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
      </Appenders>
      <Loggers>
        <Root level="WARN"><AppenderRef ref="Console"/></Root>
      </Loggers>
    </Configuration>"""
    log4j2_file = os.path.join(INSTALL_DIR_DDB, "log4j2.xml")
    save_file(log4j2_file, log4j2_config)
    run_safe(lambda: run(["zip", "-u", "DynamoDBLocal.jar", "log4j2.xml"],
                         cwd=INSTALL_DIR_DDB))

    # download agent JAR
    if not os.path.exists(DDB_AGENT_JAR_PATH):
        download(DDB_AGENT_JAR_URL, DDB_AGENT_JAR_PATH)
    if not os.path.exists(JAVASSIST_JAR_PATH):
        download(JAVASSIST_JAR_URL, JAVASSIST_JAR_PATH)
    # ensure that javassist.jar is in the manifest classpath
    run(["unzip", "-o", "DynamoDBLocal.jar", "META-INF/MANIFEST.MF"],
        cwd=INSTALL_DIR_DDB)
    manifest_file = os.path.join(INSTALL_DIR_DDB, "META-INF", "MANIFEST.MF")
    manifest = load_file(manifest_file)
    if "javassist.jar" not in manifest:
        manifest = manifest.replace("Class-Path:", "Class-Path: javassist.jar",
                                    1)
        save_file(manifest_file, manifest)
        run(["zip", "-u", "DynamoDBLocal.jar", "META-INF/MANIFEST.MF"],
            cwd=INSTALL_DIR_DDB)
示例#5
0
def lambda_su_role():
    iam_client: IAMClient = _client("iam")

    role_name = f"lambda-autogenerated-{short_uid()}"
    role = iam_client.create_role(RoleName=role_name, AssumeRolePolicyDocument=role_assume_policy)[
        "Role"
    ]
    policy_name = f"lambda-autogenerated-{short_uid()}"
    policy_arn = iam_client.create_policy(
        PolicyName=policy_name, PolicyDocument=json.dumps(role_policy_su)
    )["Policy"]["Arn"]
    iam_client.attach_role_policy(RoleName=role_name, PolicyArn=policy_arn)

    if os.environ.get("TEST_TARGET", "") == "AWS_CLOUD":  # dirty but necessary
        time.sleep(10)

    yield role["Arn"]

    run_safe(iam_client.detach_role_policy(RoleName=role_name, PolicyArn=policy_arn))
    run_safe(iam_client.delete_role(RoleName=role_name))
    run_safe(iam_client.delete_policy(PolicyArn=policy_arn))