Page MenuHomePhabricator

Create a mock vendor API endpoint at toolforge for the Security API service
Closed, ResolvedPublic

Description

Let's create a mock API endpoint under security.toolforge.org that closely mimics the vendor's API endpoint for product access based upon their current product literature.

Event Timeline

sbassett triaged this task as Medium priority.May 25 2022, 2:54 PM
sbassett moved this task from Backlog to In Progress on the iPoid-Service board.
sbassett moved this task from Backlog to In Progress on the user-sbassett board.
sbassett moved this task from Incoming to In Progress on the Security-Team board.
sbassett added a project: SecTeam-Processed.
sbassett renamed this task from Create a mock vendor API endpoint at toolforge to Create a mock vendor API endpoint at toolforge for the Security API service.May 25 2022, 4:02 PM
sbassett moved this task from In Progress to Done on the iPoid-Service board.

Ok, this should be done.

Test URL: https://security.toolforge.org/test-vendor/v2/anonymous-residential/

Code (I don't think this is important or lengthy enough to merit inclusion in version control):

.env
VENDOR_ACCESS_TOKEN="xyz"
VENDOR_FEED_PRODUCT_PATH="/path/to/file.json"
index.php
<?php

/*
 * Copyright (C) 2022  Wikimedia Foundation
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Author: sbassett@wikimedia.org
 */

/* load .env */
$dotenv = [];
$dotenv = file( __DIR__ . "/.env" );
foreach ( $dotenv as $v ) {
	$kvpairs = preg_split( "/\=/", $v );
	if ( sizeof( $kvpairs ) == 2 ) {
		define( $kvpairs[0],
			trim( str_replace( '"', "", $kvpairs[1] ) ) );
	}
}

/* check header Token */
if ( ! function_exists( 'apache_request_headers' ) ) {
	$msg = 'Invalid HTTP Request!';
	echo $msg;
	throw new \UnexpectedValueException( $msg );
}

if ( apache_request_headers()['Token'] != VENDOR_ACCESS_TOKEN ) {
	$msg = 'Invalid Access Token!';
	echo $msg;
	throw new \UnexpectedValueException( $msg );
}

/* display protected vendor file data */
if ( is_file( VENDOR_FEED_PRODUCT_PATH ) ) {
	readfile( VENDOR_FEED_PRODUCT_PATH );
	exit;
}
else {
	$msg = 'Invalid Vendor File Configuration!';
	echo $msg;
	throw new \UnexpectedValueException( $msg );
}

And the test data file can be generated via this utility script.