async onPOST()

in source/checksum/lib/api/index.js [246:347]


  async onPOST() {
    const {
      operation,
    } = this.requestPathParameters || {};

    if (operation.toLowerCase() !== APIOP_FIXITY) {
      throw new InvalidArgumentError('invalid path parameter');
    }

    const input = JSON.parse(this.requestBody);
    /* sanity check */
    const missing = [
      'Bucket',
      'Key',
      // 'Algorithm',
      // 'Expected',
      // 'ChunkSize',
      // 'StoreChecksumOnTagging',
      // 'RestoreRequest',
    ].filter(x => input[x] === undefined);
    if (missing.length) {
      throw new InvalidArgumentError(`missing ${missing.join(', ')}`);
    }

    if (!ApiRequest.isValidBucket(input.Bucket)) {
      throw new InvalidArgumentError('Invalid Bucket name');
    }

    if (!ApiRequest.isValidKey(input.Key)) {
      throw new InvalidArgumentError('Invalid Key name');
    }

    /* validate Algorithm */
    input.Algorithm = (input.Algorithm || 'md5').toLowerCase();
    if (ApiRequest.Constants.Algorithms.indexOf(input.Algorithm) < 0) {
      throw new InvalidArgumentError('invalid Algorithms parameter');
    }

    /* validate Expected */
    if (input.Expected) {
      switch (input.Algorithm) {
        /* SHA-1 expects 20 bytes (40 hex characters) */
        case 'sha1':
          if (!input.Expected.match(/^[a-fA-F0-9]{40}$/)) {
            throw new InvalidArgumentError('invalid Expected parameter');
          }
          break;
        /* MD5 expects 16 bytes (32 hex characters) */
        case 'md5':
          if (!input.Expected.match(/^[a-fA-F0-9]{32}$/)) {
            throw new InvalidArgumentError('invalid Expected parameter');
          }
          break;
        /* unsupported algorithm */
        default:
          throw new InvalidArgumentError('invalid Algorithm parameter');
      }
    }

    if (input.ChunkSize && !Number.parseInt(input.ChunkSize, 10)) {
      throw new InvalidArgumentError('invalid ChunkSize parameter');
    }

    /* validate RestoreRequest */
    if (input.RestoreRequest) {
      if (typeof input.RestoreRequest !== 'object' || Array.isArray(input.RestoreRequest)) {
        throw new InvalidArgumentError('invalid RestoreRequest parameter');
      }
      if (input.RestoreRequest.Days && !Number.parseInt(input.RestoreRequest.Days, 10)) {
        throw new InvalidArgumentError('invalid RestoreRequest parameter');
      }
      if (input.RestoreRequest.Tier
        && ApiRequest.Constants.S3.Retrieval.Tiers.indexOf(input.RestoreRequest.Tier) < 0) {
        throw new InvalidArgumentError('invalid RestoreRequest parameter');
      }
    }

    /* default to always store checksum result to tagging */
    input.StoreChecksumOnTagging = input.StoreChecksumOnTagging === undefined;

    /* arn:aws:states:eu-west-1:x:stateMachine:StateMachineName */
    const stateMachineArn = `arn:aws:states:${process.env.AWS_REGION}:${this.accountId}:stateMachine:${process.env.ENV_STATE_MACHINE_NAME}`;

    const step = new AWS.StepFunctions({
      apiVersion: '2016-11-23',
      customUserAgent: process.env.ENV_CUSTOM_USER_AGENT,
    });
    process.env.ENV_QUIET || console.log(`startExecution.input = ${JSON.stringify(input, null, 2)}`);

    const response = await step.startExecution({
      stateMachineArn,
      input: JSON.stringify(input),
    }).promise();

    const responseData = {
      statusCode: 200,
      headers: this.getCORS(),
      body: JSON.stringify(response),
    };
    process.env.ENV_QUIET || console.log(`onPOST.responseData: ${JSON.stringify(responseData, null, 2)}`);
    return responseData;
  }