Skip to content

Commit

Permalink
Merge branch 'release-2.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
hterhoeven committed Mar 4, 2020
2 parents e242e89 + a2d42f8 commit 7f52bc8
Show file tree
Hide file tree
Showing 15 changed files with 283 additions and 165 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

All notable changes to `laravel-credentials` will be documented in this file

## 2.0.0

### Added
- configuration for the editor to be used with the `credentials:edit` command

### Changed
- change the general namespace to `RtoWebsites`
- move commands and providers to distinct namespaces
- increase overall test coverage

### Fixed
- for Laravel 6 the `credentials` helper can be used in config files again

## 1.0.0 - 2018-05-15

- initial release
3 changes: 2 additions & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
The MIT License (MIT)

Copyright (c) Beyond Code GmbH <[email protected]>
Copyright (c) for portions of laravel-credentials are held by Beyond Code GmbH <[email protected]> as part of project Bar.
All other copyright for laravel-credentials are held by RTO GmbH <[email protected]>.

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
45 changes: 37 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Add encrypted credentials to your Laravel production environment

[![Latest Version on Packagist](https://img.shields.io/packagist/v/beyondcode/laravel-credentials.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-credentials)
[![Build Status](https://img.shields.io/travis/beyondcode/laravel-credentials/master.svg?style=flat-square)](https://travis-ci.org/beyondcode/laravel-credentials)
[![Quality Score](https://img.shields.io/scrutinizer/g/beyondcode/laravel-credentials.svg?style=flat-square)](https://scrutinizer-ci.com/g/beyondcode/laravel-credentials)
[![Total Downloads](https://img.shields.io/packagist/dt/beyondcode/laravel-credentials.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-credentials)
[![Latest Version on Packagist](https://img.shields.io/packagist/v/rto-websites/laravel-credentials.svg?style=flat-square)](https://packagist.org/packages/rto-websites/laravel-credentials)
[![Build Status](https://img.shields.io/travis/rto-websites/laravel-credentials/master.svg?style=flat-square)](https://travis-ci.org/rto-websites/laravel-credentials)
[![Quality Score](https://img.shields.io/scrutinizer/g/rto-websites/laravel-credentials.svg?style=flat-square)](https://scrutinizer-ci.com/g/rto-websites/laravel-credentials)
[![Total Downloads](https://img.shields.io/packagist/dt/rto-websites/laravel-credentials.svg?style=flat-square)](https://packagist.org/packages/rto-websites/laravel-credentials)

The `beyondcode/laravel-credentials` package allows you to store all your secret credentials in an encrypted file and put that file into version control instead of
Since the [original package](https://github.com/beyondcode/laravel-credentials) does not seem to be maintained any more, this is an adaptation of the `beyondcode/laravel-credentials` package. See [here](#migration) how to migrate.

The `rto-websites/laravel-credentials` package allows you to store all your secret credentials in an encrypted file and put that file into version control instead of
having to add multiple credentials into your `.env` file in your production environment.

There are a couple of benefits of using encrypted credentials instead of environment keys:
Expand All @@ -32,22 +34,22 @@ With the built-in edit command, you can easily edit your existing credentials. T
```bash
php artisan credentials:edit
```
![Credentials Demo](https://beyondco.de/github/credentials.gif)
![Credentials Demo](https://github.com/RTO-Websites/laravel-credentials/blob/master/credentials.gif)

## Installation

You can install the package via composer:

```bash
composer require beyondcode/laravel-credentials
composer require rto-websites/laravel-credentials
```

The package will automatically register itself.

You can optionally publish the configuration with:

```bash
php artisan vendor:publish --provider="BeyondCode\Credentials\CredentialsServiceProvider" --tag="config"
php artisan vendor:publish --provider="RtoWebsites\Credentials\CredentialsServiceProvider" --tag="config"
```

This is the content of the published config file:
Expand All @@ -62,6 +64,11 @@ return [
*/
'file' => config_path('credentials.php.enc'),

/**
*
*/
'editor' => env('CREDENTIALS_EDITOR', 'vi'),

/*
* Defines the key that will be used to encrypt / decrypt the credentials.
* The default is your application key. Be sure to keep this key secret!
Expand All @@ -73,6 +80,28 @@ return [
];
```

### Migration

If you are about to move over from the [original package](https://github.com/beyondcode/laravel-credentials)
to this one you may need to make some minor adjustments to your project.

In most cases all you have to do are the following three steps:
1. run `composer require rto-websites/laravel-credentials`
1. remove `beyondcode/laravel-credentials` from you `composer.json`
1. run `composer update beyondcode/laravel-credentials`

If you published the config file you will need to add this line to your `config/credentials.php`:

```php
'editor' => env('CREDENTIALS_EDITOR', 'vi'),
```

If you are not using the package auto-discovery and registering the service provider by hand
you will need make a small adjustment to your registration since the service provider's namespace
has changed.
Go to your `config/app.php` and replace
`BeyondCode\Credentials\CredentialsServiceProvider::class` with `RtoWebsites\Credentials\CredentialsServiceProvider::class`.

### Testing

``` bash
Expand Down
22 changes: 16 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
{
"name": "beyondcode/laravel-credentials",
"name": "rto-websites/laravel-credentials",
"description": "Store and access encrypted credentials in your Laravel application.",
"type": "library",
"keywords": [
"beyondcode",
"rto-websites",
"laravel-credentials"
],
"homepage": "https://github.com/beyondcode/laravel-credentials",
"homepage": "https://github.com/RTO-Websites/laravel-credentials",
"license": "MIT",
"support": {
"issues": "https://github.com/RTO-Websites/laravel-credentials/issues",
"source": "https://github.com/RTO-Websites/laravel-credentials"
},
"authors": [
{
"name": "RTO Websites",
"email": "[email protected]"
},
{
"name": "Marcel Pociot",
"email": "[email protected]",
Expand All @@ -17,6 +26,7 @@
],
"require": {
"php": "^7.1",
"ext-json": "*",
"illuminate/encryption": "5.6.*|5.7.*|5.8.*|^6.0"
},
"require-dev": {
Expand All @@ -25,15 +35,15 @@
},
"autoload": {
"psr-4": {
"BeyondCode\\Credentials\\": "src"
"RtoWebsites\\Credentials\\": "src"
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"BeyondCode\\Credentials\\Tests\\": "tests"
"RtoWebsites\\Credentials\\Tests\\": "tests"
}
},
"scripts": {
Expand All @@ -47,7 +57,7 @@
"extra": {
"laravel": {
"providers": [
"BeyondCode\\Credentials\\CredentialsServiceProvider"
"RtoWebsites\\Credentials\\CredentialsServiceProvider"
]
}
}
Expand Down
5 changes: 5 additions & 0 deletions config/credentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
*/
'file' => config_path('credentials.php.enc'),

/**
*
*/
'editor' => env('CREDENTIALS_EDITOR', 'vi'),

/*
* Defines the key that will be used to encrypt / decrypt the credentials.
* The default is your application key. Be sure to keep this key secret!
Expand Down
Binary file added credentials.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="BeyondCode Test Suite">
<testsuite name="RtoWebsites Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
Expand All @@ -25,7 +25,7 @@
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

namespace BeyondCode\Credentials;
namespace RtoWebsites\Credentials\Commands;

use Illuminate\Console\Command;
use RtoWebsites\Credentials\Credentials;
use RtoWebsites\Credentials\Exceptions\InvalidJSON;
use Symfony\Component\Process\Process;
use BeyondCode\Credentials\Exceptions\InvalidJSON;

class EditCredentialsCommand extends Command
{
Expand All @@ -25,8 +26,7 @@ class EditCredentialsCommand extends Command
/**
* The command handler.
*
* @param \BeyondCode\Credentials\Credentials $credentials
* @return void
* @param Credentials $credentials
*/
public function handle(Credentials $credentials)
{
Expand All @@ -39,12 +39,7 @@ public function handle(Credentials $credentials)

fwrite($handle, json_encode($decrypted, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT));

$editor = env('EDITOR', 'vi');

$process = new Process($editor.' '.$meta['uri']);

$process->setTty(true);
$process->mustRun();
$this->runEditor($meta['uri']);

$data = json_decode(file_get_contents($meta['uri']), JSON_OBJECT_AS_ARRAY);

Expand All @@ -56,4 +51,16 @@ public function handle(Credentials $credentials)

$this->info('Successfully updated credentials.');
}

/**
* @param string $argument
*/
public function runEditor(string $argument): void
{
$editor = config('credentials.editor');

$process = new Process([$editor, $argument]);
$process->setTty(true);
$process->mustRun();
}
}
3 changes: 1 addition & 2 deletions src/Credentials.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

namespace BeyondCode\Credentials;
namespace RtoWebsites\Credentials;

use Illuminate\Support\Arr;
use Illuminate\Contracts\Encryption\Encrypter;
use BeyondCode\Credentials\Exceptions\FileDoesNotExist;

class Credentials
{
Expand Down
20 changes: 11 additions & 9 deletions src/CredentialsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

namespace BeyondCode\Credentials;
namespace RtoWebsites\Credentials;

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Encryption\Encrypter;
use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use RtoWebsites\Credentials\Commands\EditCredentialsCommand;

class CredentialsServiceProvider extends ServiceProvider
{
Expand All @@ -16,14 +17,15 @@ class CredentialsServiceProvider extends ServiceProvider
*/
public function boot()
{
$configFile = dirname(__DIR__) . '/config/credentials.php';
$this->publishes([
__DIR__ . '/../config/credentials.php' => config_path('credentials.php'),
$configFile => config_path('credentials.php'),
], 'config');

$this->mergeConfigFrom(__DIR__ . '/../config/credentials.php', 'credentials');
$this->mergeConfigFrom($configFile, 'credentials');

// Update configuration strings
if( !app()->configurationIsCached()) {
if (!app()->configurationIsCached()) {
$this->fixConfig();
}
}
Expand All @@ -38,7 +40,7 @@ protected function fixConfig()
collect(Arr::dot(config()->all()))->filter(function ($item) {
return is_string($item) && Str::startsWith($item, Credentials::CONFIG_PREFIX);
})->map(function ($item, $key) {
$item = str_replace_first(Credentials::CONFIG_PREFIX, '', $item);
$item = Str::replaceFirst(Credentials::CONFIG_PREFIX, '', $item);

config()->set($key, credentials($item));
});
Expand All @@ -51,7 +53,7 @@ protected function fixConfig()
*/
public function register()
{
$this->app->bind(Credentials::class, function(){
$this->app->bind(Credentials::class, function () {

// If the key starts with "base64:", we will need to decode the key before handing
// it off to the encrypter. Keys may be base-64 encoded for presentation and we
Expand All @@ -68,7 +70,7 @@ public function register()
$this->app->bind('command.credentials.edit', EditCredentialsCommand::class);

$this->commands([
'command.credentials.edit'
'command.credentials.edit',
]);
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidJSON.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace BeyondCode\Credentials\Exceptions;
namespace RtoWebsites\Credentials\Exceptions;

use Exception;

Expand Down
6 changes: 4 additions & 2 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use BeyondCode\Credentials\Credentials;
use Illuminate\Contracts\Container\BindingResolutionException;
use RtoWebsites\Credentials\Credentials;

if (! function_exists('credentials')) {
/**
Expand All @@ -15,11 +16,12 @@ function credentials(string $key, $default = null)
$filename = config('credentials.file');

try {
/* @var Credentials $credentials */
$credentials = app(Credentials::class);
$credentials->load($filename);

return $credentials->get($key, $default);
} catch (ReflectionException $e) {
} catch (ReflectionException|BindingResolutionException $e) {
return Credentials::CONFIG_PREFIX.$key;
}
}
Expand Down
Loading

0 comments on commit 7f52bc8

Please sign in to comment.