Last active
August 29, 2015 14:02
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 )) | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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