Skip to content

Commit

Permalink
Custom masked fields
Browse files Browse the repository at this point in the history
In this update we've cleanup up the code and allowed you to define a list of custom keywords that will be masked before leaving your API. We also made our Readme better so you can get started even quicker.
  • Loading branch information
cindreta committed May 23, 2021
1 parent 9e07b64 commit 4b55cf6
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 50 deletions.
2 changes: 1 addition & 1 deletion LICENCE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) Treblle INC. <vedran@flip.hr>
Copyright (c) Treblle Limited. <vedran@treblle.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
56 changes: 39 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@

# Treblle for PHP
Treblle makes it super easy to understand what’s going on with your APIs and the apps that use them. To get started with Treblle create a FREE account on <https://treblle.com>.

[![Latest Version](https://img.shields.io/packagist/v/treblle/treblle-php)](https://packagist.org/packages/treblle/treblle-php)
[![Total Downloads](https://img.shields.io/packagist/dt/treblle/treblle-php)](https://packagist.org/packages/treblle/treblle-php)
[![MIT Licence](https://img.shields.io/packagist/l/treblle/treblle-php)](LICENSE.md)

Treblle makes it super easy to understand what’s going on with your APIs and the apps that use them. Just by adding Treblle to your API out of the box you get:
* Real-time API monitoring and logging
* Auto-generated API docs with OAS support
* API analytics
* Quality scoring
* One-click testing
* API management on the go
* and more...

## Requirements
* PHP 5.5+
Expand All @@ -8,37 +21,46 @@ Treblle makes it super easy to understand what’s going on with your APIs and t
* [`guzzlehttp/guzzle`](https://packagist.org/packages/guzzlehttp/guzzle)

## Installation
You can install Treblle via [Composer](http://getcomposer.org/). Simply run the following command:
You can install Treblle for PHP via [Composer](http://getcomposer.org/). Simply run the following command:
```bash
$ composer require treblle/treblle-php
```
Don't forget to [autoload](https://getcomposer.org/doc/01-basic-usage.md#autoloading) composer to your project by including the following code:

```php
require_once('vendor/autoload.php');
```

## Getting started
The first thing you need to do is create a FREE account on <https://treblle.com> to get an API key and Project ID. After that all you need to do is add the following line of code to your PHP API project:
Next, create a FREE account on <https://treblle.com> to get an API key and Project ID. After you have those simply initialize Treblle in your API code like so:

```php
$treblle = new Treblle\Treblle('YOUR_API_KEY', 'YOUR_PROJECT_ID');
<?php
// DON'T FORGET TO AUTOLOAD COMPOSER DEPENDENCIES
require_once("vendor/autoload.php");

// INITIALIZE TREBLLE
$treblle = new Treblle\Treblle('_YOUR_API_KEY_', '_YOUR_PROJECT_ID_');
```
That's it. Your API requests and responses are now being sent to your Treblle project. Just by adding that line of code you get features like: auto-documentation, real-time request/response monitoring, error tracking and so much more.

Treblle will catch everything that is sent to your API endpoints as well as everything that the endpoints return. In case you wish to add even more information to track specific things in your API but NOT return them in the response you can call add meta information to a specific API endpoint or all endpoints. To do so you can do the following:

## Configuration options
Treblle **masks sensitive information** from the request parameters **before it even leaves your server**. The following parameters are automatically masked: password, pwd, secret, password_confirmation, cc, card_number, ccv, ssn, credit_score. You can extend this list by providing your own custom keywords by doing the following:

```php
$treblle = new Treblle\Treblle('YOUR_API_KEY', 'YOUR_PROJECT_ID');
$treblle->addMeta('pricing', array('price_per_item' => 100, 'number_of_items' => '2', 'total' => 200));
```
<?php
// DON'T FORGET TO AUTOLOAD COMPOSER DEPENDENCIES
require_once("vendor/autoload.php");

The setMeta method takes in two parameters. The first one is the name of your meta information and the second one is an array where you can add ANY information you want. Treblle will make sure that this is attached to the request and you will always be able to see it and search for it.
/*
* Pass an array of words that you would like to be masked
* as a third parameter when initializing Treblle
*/
$treblle = new Treblle\Treblle(
'_YOUR_API_KEY_',
'_YOUR_PROJECT_ID_',
['keyword', 'maskme', 'sensitive']
);
```

## Support
If you have problems adding, installing or using Treblle feel free to reach out via <https://treblle.com> or contact vedran@flip.hr and we will make sure to do a FREE integration for you.
If you have problems of any kind feel free to reach out via <https://treblle.com> or email vedran@treblle.com and we'll do our best to help you out.

## License
Copyright 2020, Treblle. Licensed under the MIT license:
Copyright 2021, Treblle Limited. Licensed under the MIT license:
http://www.opensource.org/licenses/mit-license.php
24 changes: 12 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
{
"type": "library",
"name": "treblle/treblle-laravel",
"type": "library",
"name": "treblle/treblle-php",
"description" : "Stay in tune with your APIs",
"homepage": "https://treblle.com/",
"keywords": [
"treblle",
"api",
"monitoring",
"logging",
"phpunit",
"treblle",
"api",
"monitoring",
"debuging",
"documentation",
"developer tools"
"documentation"
],
"license": "MIT",
"authors": [
{
"name": "Vedran Cindrić",
"email": "vedran@flip.hr",
"email": "vedran@treblle.com",
"homepage": "https://treblle.com/",
"role": "Developer"
}
],
"license": "MIT",
"require": {
"php": "^5.5|^6.0|^7.0|^8.0",
"guzzlehttp/guzzle": "^5.0|^6.0|^7.0"
},
"autoload": {
"psr-4": {
"psr-4" : {
"Treblle\\": "src/"
}
},
"config": {
"preferred-install": "dist"
"preferred-install": "dist",
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
Expand Down
51 changes: 31 additions & 20 deletions src/Treblle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Treblle {
private $api_key;

/**
* Create a FREE Treblle account => https://treblle.com/register
* Your Treblle Project ID
* @var string
*/
private $project_id;
Expand All @@ -27,12 +27,23 @@ class Treblle {
*/
private $guzzle;

/**
* Default fields that will be masked
* @var array
*/
private $masked = [
'password', 'pwd', 'secret', 'password_confirmation', 'cc', 'card_number', 'ccv', 'ssn',
'credit_score'
];

/**
* Create a new Treblle instance
* @param string $api_key
* @param string $project
* @param array $custom_fields
* @return void
*/
public function __construct($api_key = null, $project_id = null) {
public function __construct($api_key = null, $project_id = null, $custom_fields = null) {

error_reporting(E_ALL);

Expand All @@ -44,15 +55,20 @@ public function __construct($api_key = null, $project_id = null) {
throw new \Exception('Please provide a valid Treblle Project ID.');
}

$this->api_key = $api_key;
$this->project_id = $project_id;

if(!class_exists('\GuzzleHttp\Client')) {
throw new \Exception('Treblle needs the Guzzle HTTP client to work. Please run: composer require guzzlehttp/guzzle');
}

$this->api_key = $api_key;
$this->project_id = $project_id;
$this->guzzle = new \GuzzleHttp\Client;

if(is_array($custom_fields)) {
if(!empty($custom_fields)) {
$this->masked = array_unique(array_merge($this->masked, $custom_fields));
}
}

$this->payload = [
'api_key' => $this->api_key,
'project_id' => $this->project_id,
Expand All @@ -61,7 +77,7 @@ public function __construct($api_key = null, $project_id = null) {
'data' => [
'server' => [
'ip' => $this->getServerVariable('SERVER_ADDR'),
'timezone' => $this->getTimezone(),
'timezone' => $this->getTimezone(),
'os' => [
'name' => php_uname('s'),
'release' => php_uname('r'),
Expand Down Expand Up @@ -207,8 +223,8 @@ public function onShutdown() {
}

$this->guzzle->request('POST', 'https://rocknrolla.treblle.com', [
'connect_timeout' => 10,
'timeout' => 10,
'connect_timeout' => 3,
'timeout' => 3,
'verify' => false,
'headers' => [
'Content-Type' => 'application/json',
Expand Down Expand Up @@ -316,14 +332,14 @@ public function getTimestamp() {
* return @string
*/
public function getTimezone() {
$timezone = 'UTC';
$timezone = 'UTC';

if (ini_get('date.timezone')) {
$timezone = ini_get('date.timezone');
}
if (ini_get('date.timezone')) {
$timezone = ini_get('date.timezone');
}

return $timezone;
return $timezone;
}


Expand Down Expand Up @@ -398,11 +414,6 @@ public function translateErrorType($type) {
* @return array
*/
public function maskFields($data) {

$fields = [
'password', 'pwd', 'secret', 'password_confirmation', 'cc', 'card_number', 'ccv', 'ssn',
'credit_score'
];

if(!is_array($data)) {
return;
Expand All @@ -413,7 +424,7 @@ public function maskFields($data) {
if(is_array($value)) {
$this->maskFields($data[$key]);
} else {
foreach ($fields as $field) {
foreach ($this->masked as $field) {

if(preg_match('/\b'.$field.'\b/mi', $key)) {
$data[$key] = str_repeat('*', strlen($value));
Expand Down

0 comments on commit 4b55cf6

Please sign in to comment.