set ( $credential_sets )

Stores the credentials for re-use.

Access

public static

Parameters

Parameter

Type

Required

Description

$credential_sets

array

Required

The named credential sets that should be made available to the application.

Examples

Set the credential sets that should be available to the application, and read back the values.

// Dependencies
require_once dirname(__FILE__) . '/../../sdk.class.php';
require_once dirname(__FILE__) . '/../testutil.inc.php';

/**
 * Create a group of credential sets. This would normally go inside the config.inc.php file.
 *
 * Each credential set is assigned a name. Any name that PHP allows as an array key may be used.
 * `@inherit` is a keyword that will tell the class to use the inherited credential set as a base,
 * then apply changes on top of it. The "default" credential set can be its own credential set,
 * or can reference another credential set by name.
 */
CFCredentials::set(array(

	// Credentials for the production environment
	'production' => array(
		'key' => 'production-key',
		'secret' => 'production-secret',
		'cache' => 'apc',
		'something_else' => 'abc123',
		'aardvark' => 'applesauce'
	),

	// Credentials for the staging environment. Inherits from "production".
	'staging' => array(
		'@inherit' => 'production',
		'key' => 'staging-key',
		'secret' => 'staging-secret',
	),

	// Credentials for the development environment. Inherits from "staging", and in-turn, "production".
	'development' => array(
		'@inherit' => 'staging',
		'key' => 'development-key',
		'secret' => 'development-secret',
	),

	// Any name can be used for a credential set.
	'my-own-personal-credential-set' => array(
		'@inherit' => 'staging'
	),

	// Set a default credential set for ambiguous cases.
	'@default' => 'development'
));

/**
 * Requesting data back from the credential sets.
 */
echo CFCredentials::get('production')->key . PHP_EOL;
echo CFCredentials::get('production')->aardvark . PHP_EOL;

echo PHP_EOL;

echo CFCredentials::get('staging')->key . PHP_EOL;
echo CFCredentials::get('staging')->aardvark . PHP_EOL;

echo PHP_EOL;

echo CFCredentials::get('development')->key . PHP_EOL;
echo CFCredentials::get('development')->aardvark . PHP_EOL;

echo PHP_EOL;

echo CFCredentials::get('my-own-personal-credential-set')->key . PHP_EOL;

echo PHP_EOL;

echo CFCredentials::get()->key . PHP_EOL;
Result:
production-key
applesauce

staging-key
applesauce

development-key
applesauce

staging-key

development-key

Source

Method defined in utilities/credentials.class.php | Toggle source view (47 lines) | View on GitHub

public static function set(array $credential_sets)
{
    // Make sure a default credential set is specified or can be inferred
    if (count($credential_sets) === 1)
    {
        $credential_sets[self::DEFAULT_KEY] = reset($credential_sets);
    }

    // Resolve any @inherit tags
    foreach ($credential_sets as $credential_name => &$credential_set)
    {
        if (is_array($credential_set))
        {
            foreach ($credential_set as $credential_key => &$credential_value)
            {
                if ($credential_key === self::INHERIT_KEY)
                {
                    if (!isset($credential_sets[$credential_value]))
                    {
                        throw new CFCredentials_Exception('The credential set, "' . $credential_value . '", does not exist and cannot be inherited.');
                    }

                    $credential_set = array_merge($credential_sets[$credential_value], $credential_set);
                    unset($credential_set[self::INHERIT_KEY]);
                }
            }
        }
    }

    // Normalize the value of the @default credential set
    if (isset($credential_sets[self::DEFAULT_KEY]))
    {
        $default = $credential_sets[self::DEFAULT_KEY];
        if (is_string($default))
        {
            if (!isset($credential_sets[$default]))
            {
                throw new CFCredentials_Exception('The credential set, "' . $default . '", does not exist and cannot be used as the default credential set.');
            }

            $credential_sets[self::DEFAULT_KEY] = $credential_sets[$default];
        }
    }

    // Store the credentials
    self::$credentials = $credential_sets;
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback