Skip to content

Commit

Permalink
Merge pull request #21 from razorpay/f/20
Browse files Browse the repository at this point in the history
[trace] Adds debug message for exceptions during lease creation
  • Loading branch information
captn3m0 committed Nov 21, 2017
2 parents 49d4422 + 511200b commit 6c59537
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
55 changes: 46 additions & 9 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Http\Controllers;

use App;
use Log;
use Illuminate\Support\Facades\Log;
use Auth;
use Mail;
use View;
Expand Down Expand Up @@ -476,7 +476,9 @@ public function getInvite($token)
'expiry' => $invite->expiry,
'invite_email'=> $email,
];

$result = $this->createLease($lease);

if (! $result) {
//Lease Creation Failed. AWS Reported an error. Generally in case if a lease with same ip, protocl, port already exists on AWS.
return View::make('pages.guest')->with('failure', "Error encountered while creating lease. Please try again. If doesn't help contact the admin.");
Expand Down Expand Up @@ -521,18 +523,20 @@ public function postAddUser()

//Validation Rules
$user_rules = [
'email' => 'required|between:2,50|email|unique:users|org_email',
'name' => 'required|between:3,100',
'admin' => 'required|in:1,0',
'email' => 'required|between:2,50|email|unique:users|org_email',
'name' => 'required|between:3,100',
'admin' => 'required|in:1,0',
];

$domain = config('concierge.google_domain');

$validator = Validator::make($input, $user_rules, [
'org_email' => "Only {config('concierge.google_domain')} emails allowed",
'org_email' => "Only $domain emails allowed",
]);

if ($validator->fails()) {
return redirect('/users/add')
->with('errors', $validator->messages()->toArray());
->with('errors', $validator->messages()->toArray());
} else {

// Backward compatible
Expand All @@ -557,13 +561,13 @@ public function postEditUser($id)

//Validation Rules
$user_rules = [
'email' => "required|between:2,50|email|unique:users,email,$id|org",
'email' => "required|between:2,50|email|unique:users,email,$id|org_email",
'name' => 'required|between:3,100|alpha_spaces',
'admin' => 'required|in:1,0',
];

$validator = Validator::make($input, $user_rules, [
'org' => 'Only razorpay.com emails allowed',
'org_email' => 'Only razorpay.com emails allowed',
]);

if ($validator->fails()) {
Expand Down Expand Up @@ -663,6 +667,10 @@ private function createLease($lease)
'CidrIp' => $lease['lease_ip'],
]);
} catch (Exception $e) {
Log::info('Error while creating lease', [
'lease' => $lease,
'exception' => $e->getMessage(),
]);
return false;
}

Expand Down Expand Up @@ -696,8 +704,37 @@ private function terminateLease($lease)
private function getClientIp()
{
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) and $_SERVER['HTTP_X_FORWARDED_FOR']) {
// if behind an ELB
// if behind an load balancer, assume that all load balancers have private IPs
// and the first public IP will be that of the client
$clientIpAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];

// We pick the first non-public IP we get

if (strpos($clientIpAddress, ',') !== false)
{
$ips = array_reverse(array_map('trim', explode(',' , $clientIpAddress)));

function isPublicIp($ip)
{
$flags = FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;

return (bool) filter_var(
$ip,
FILTER_VALIDATE_IP,
[
'flags' => $flags,
]
);
}

foreach ($ips as $ip)
{
if (isPublicIp($ip))
{
return $ip;
}
}
}
} else {
// if not behind ELB
$clientIpAddress = $_SERVER['REMOTE_ADDR'];
Expand Down
1 change: 0 additions & 1 deletion config/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ private function configureValidator()
$email_domain = end($email_parts);

return ($email_domain === $domain);

});

}
Expand Down

0 comments on commit 6c59537

Please sign in to comment.