Skip to content

Instantly share code, notes, and snippets.

@martijngastkemper
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martijngastkemper/8ed4efb46d03ec26e59a to your computer and use it in GitHub Desktop.
Save martijngastkemper/8ed4efb46d03ec26e59a to your computer and use it in GitHub Desktop.
I was searching for a solution to make key files available in my Heroku app without adding it to the repository. Using a streamwrapper doesn't work, because ssh2 only accepts strings to files without a schema part. But still want to share this very useless class with you. "echo file_get_contents( 'env://ENV_VAR' );" does exactly the same as "get…
<?php
class EnvStream
{
private $var, $position = 0;
function stream_open($path, $mode, $options, &$opened_path)
{
$this->var = parse_url( $path, PHP_URL_HOST );
return true;
}
function stream_read($count)
{
if( $this->position > 0 )
return null;
$ret = getenv( $this->var );
$this->position += strlen( $ret );
return $ret;
}
function stream_seek($offset, $whence)
{
switch ($whence) {
case SEEK_SET:
if ($offset < strlen( getenv( $this->var )) && $offset >= 0) {
$this->position = $offset;
return true;
} else {
return false;
}
break;
case SEEK_CUR:
if ($offset >= 0) {
$this->position += $offset;
return true;
} else {
return false;
}
break;
case SEEK_END:
if (strlen(getenv( $this->var )) + $offset >= 0) {
$this->position = strlen(getenv( $this->var )) + $offset;
return true;
} else {
return false;
}
break;
default:
return false;
}
}
function stream_eof()
{
return $this->position >= strlen( getenv( $this->var ));
}
function stream_tell()
{
return $this->position;
}
function stream_stat()
{
return [
'size' => strlen( getenv( $this->var ))
];
}
}
<?php
include( 'EnvStream.php' );
stream_wrapper_register("env", "EnvStream")
or die("Failed to register protocol");
echo file_get_contents( "env://ENV_VAR" ); // Prints content of ENV_VAR
$connection = ssh2_connect( 'your.server', 22 );
// Prints the error "Unable to open public key file"
ssh2_auth_pubkey_file( $connection,
'root',
'env://PUBLIC_KEY',
'env://PRIVATE_KEY'
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment