Hướng dẫn php secrets manager - quản lý bí mật php

Phiên bản này của danh mục mẫu mã AWS đã được thay thế bằng thư viện mã AWS, trong đó có các ví dụ mã mới và cập nhật.

Các ví dụ được liệt kê trên trang này là các mẫu mã được viết trong PHP trình bày cách tương tác với Trình quản lý bí mật AWS.

Để biết thêm thông tin, hãy xem AWS SDK cho Hướng dẫn phát triển PHP và tham chiếu API của Trình quản lý bí mật AWS.

  • AddNewSecretValue.php
  • AttachLabel.php
  • CreateSecret.php
  • CreateSecretRotation.php
  • DeleteSecret.php
  • DescribeSecret.php
  • GetSecretValue.php
  • ListSecretVersions.php
  • ListSecrets.php
  • RemoveLabel.php
  • RestoreSecret.php
  • RotateSecret.php

Điều này tạo ra một phiên bản mới của bí mật. Nếu một phiên bản của bí mật đã tồn tại, hãy thêm tham số

$client = new SecretsManagerClient([
    'profile' => 'default',
    'version' => '2017-10-17',
    'region' => 'us-west-2'
]);

$secretName = '<<{{MySecretName}}>>';
$secret = '{"username":"<<USERNAME>>","password":"<<PASSWORD>>"}';
$description = '<<Description>>';

try {
    $result = $client->createSecret([
        'Description' => $description,
        'Name' => $secretName,
        'SecretString' => $secret,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
 
8 với giá trị trong
$client = new SecretsManagerClient([
    'profile' => 'default',
    'version' => '2017-10-17',
    'region' => 'us-west-2'
]);

$secretName = '<<{{MySecretName}}>>';
$secret = '{"username":"<<USERNAME>>","password":"<<PASSWORD>>"}';
$description = '<<Description>>';

try {
    $result = $client->createSecret([
        'Description' => $description,
        'Name' => $secretName,
        'SecretString' => $secret,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
 
9 để đảm bảo rằng giá trị mới được sử dụng khi truy xuất giá trị.

Xoay giá trị cho một bí mật hiện có trong Trình quản lý bí mật

Để xoay giá trị của một bí mật hiện có được lưu trữ trong Trình quản lý bí mật, hãy sử dụng chức năng xoay Lambda và hoạt động Rotatesecret.

  • Tạo một bí mật bằng Createsecret.

  • Lấy một bí mật bằng cách sử dụng getSecretValue.

  • Liệt kê tất cả các bí mật được lưu trữ bởi Trình quản lý bí mật bằng Danh sách.

  • Nhận chi tiết về một bí mật được chỉ định bằng cách sử dụng mô tả.

  • Cập nhật một bí mật được chỉ định bằng cách sử dụng PutSecretValue.

  • Thiết lập một vòng quay bí mật bằng cách sử dụng Rotatesecret.

  • Đánh dấu một bí mật để xóa bằng cách sử dụng DeleteSecret.

Tất cả các mã ví dụ cho AWS SDK cho PHP có sẵn ở đây trên GitHub.here on GitHub.

Thông tin xác thực

Trước khi chạy mã ví dụ, định cấu hình thông tin đăng nhập AWS của bạn, như được mô tả trong việc thiết lập thông tin đăng nhập. Sau đó nhập AWS SDK cho PHP, như được mô tả trong việc sử dụng cơ bản.

Tạo bí mật trong Trình quản lý bí mật

Để tạo bí mật trong Trình quản lý bí mật, hãy sử dụng thao tác Createsecret.

Trong ví dụ này, tên người dùng và mật khẩu được lưu trữ dưới dạng chuỗi JSON.

Nhập khẩu


require 'vendor/autoload.php';

use Aws\SecretsManager\SecretsManagerClient; 
use Aws\Exception\AwsException;

Mã mẫu

$client = new SecretsManagerClient([
    'profile' => 'default',
    'version' => '2017-10-17',
    'region' => 'us-west-2'
]);

$secretName = '<<{{MySecretName}}>>';
$secret = '{"username":"<<USERNAME>>","password":"<<PASSWORD>>"}';
$description = '<<Description>>';

try {
    $result = $client->createSecret([
        'Description' => $description,
        'Name' => $secretName,
        'SecretString' => $secret,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
 

Lấy bí mật từ người quản lý bí mật

Để lấy lại giá trị của một bí mật được lưu trữ trong Trình quản lý bí mật, hãy sử dụng thao tác GetSecretValue.

Trong ví dụ này, bí mật là một chuỗi chứa giá trị được lưu trữ. Nếu được gọi vào bí mật chúng tôi đã tạo trước đó, mẫu này sẽ xuất ra

$client = new SecretsManagerClient([
    'profile' => 'default',
    'version' => '2017-10-17',
    'region' => 'us-west-2'
]);

$secretName = '<<{{MySecretName}}>>';
$secret = '{"username":"<<USERNAME>>","password":"<<PASSWORD>>"}';
$description = '<<Description>>';

try {
    $result = $client->createSecret([
        'Description' => $description,
        'Name' => $secretName,
        'SecretString' => $secret,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
 
6. Sử dụng
$client = new SecretsManagerClient([
    'profile' => 'default',
    'version' => '2017-10-17',
    'region' => 'us-west-2'
]);

$secretName = '<<{{MySecretName}}>>';
$secret = '{"username":"<<USERNAME>>","password":"<<PASSWORD>>"}';
$description = '<<Description>>';

try {
    $result = $client->createSecret([
        'Description' => $description,
        'Name' => $secretName,
        'SecretString' => $secret,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
 
7 để truy cập các giá trị được lập chỉ mục.

Nhập khẩu


require 'vendor/autoload.php';

use Aws\SecretsManager\SecretsManagerClient; 
use Aws\Exception\AwsException;

Mã mẫu

$client = new SecretsManagerClient([
    'profile' => 'default',
    'version' => '2017-10-17',
    'region' => '<<{{MyRegionName}}>>',
]);

$secretName = '<<{{MySecretName}}>>';

try {
    $result = $client->getSecretValue([
        'SecretId' => $secretName,
    ]);

} catch (AwsException $e) {
    $error = $e->getAwsErrorCode();
    if ($error == 'DecryptionFailureException') {
        // Secrets Manager can't decrypt the protected secret text using the provided AWS KMS key.
        // Handle the exception here, and/or rethrow as needed.
        throw $e;
    }
    if ($error == 'InternalServiceErrorException') {
        // An error occurred on the server side.
        // Handle the exception here, and/or rethrow as needed.
        throw $e;
    }
    if ($error == 'InvalidParameterException') {
        // You provided an invalid value for a parameter.
        // Handle the exception here, and/or rethrow as needed.
        throw $e;
    }
    if ($error == 'InvalidRequestException') {
        // You provided a parameter value that is not valid for the current state of the resource.
        // Handle the exception here, and/or rethrow as needed.
        throw $e;
    }
    if ($error == 'ResourceNotFoundException') {
        // We can't find the resource that you asked for.
        // Handle the exception here, and/or rethrow as needed.
        throw $e;
    }
}
// Decrypts secret using the associated KMS CMK.
// Depending on whether the secret is a string or binary, one of these fields will be populated.
if (isset($result['SecretString'])) {
    $secret = $result['SecretString'];
} else {
    $secret = base64_decode($result['SecretBinary']);
}

// Your code goes here; 
 

Lấy bí mật từ người quản lý bí mật

Để lấy lại giá trị của một bí mật được lưu trữ trong Trình quản lý bí mật, hãy sử dụng thao tác GetSecretValue.

Nhập khẩu


require 'vendor/autoload.php';

use Aws\SecretsManager\SecretsManagerClient; 
use Aws\Exception\AwsException;

Mã mẫu

$client = new SecretsManagerClient([
    'profile' => 'default',
    'version' => '2017-10-17',
    'region' => 'us-west-2'
]);

try {
    $result = $client->listSecrets([
    ]);
    var_dump($result);
}  catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
 

Lấy bí mật từ người quản lý bí mật

Để lấy lại giá trị của một bí mật được lưu trữ trong Trình quản lý bí mật, hãy sử dụng thao tác GetSecretValue.

Nhập khẩu


require 'vendor/autoload.php';

use Aws\SecretsManager\SecretsManagerClient; 
use Aws\Exception\AwsException;

Mã mẫu

$client = new SecretsManagerClient([
    'profile' => 'default',
    'version' => '2017-10-17',
    'region' => 'us-west-2'
]);

$secretName = '<<{{MySecretName}}>>';

try {
    $result = $client->describeSecret([
        'SecretId' => $secretName,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}

 

Lấy bí mật từ người quản lý bí mật

Để lấy lại giá trị của một bí mật được lưu trữ trong Trình quản lý bí mật, hãy sử dụng thao tác GetSecretValue.

Trong ví dụ này, bí mật là một chuỗi chứa giá trị được lưu trữ. Nếu được gọi vào bí mật chúng tôi đã tạo trước đó, mẫu này sẽ xuất ra

$client = new SecretsManagerClient([
    'profile' => 'default',
    'version' => '2017-10-17',
    'region' => 'us-west-2'
]);

$secretName = '<<{{MySecretName}}>>';
$secret = '{"username":"<<USERNAME>>","password":"<<PASSWORD>>"}';
$description = '<<Description>>';

try {
    $result = $client->createSecret([
        'Description' => $description,
        'Name' => $secretName,
        'SecretString' => $secret,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
 
6. Sử dụng
$client = new SecretsManagerClient([
    'profile' => 'default',
    'version' => '2017-10-17',
    'region' => 'us-west-2'
]);

$secretName = '<<{{MySecretName}}>>';
$secret = '{"username":"<<USERNAME>>","password":"<<PASSWORD>>"}';
$description = '<<Description>>';

try {
    $result = $client->createSecret([
        'Description' => $description,
        'Name' => $secretName,
        'SecretString' => $secret,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
 
7 để truy cập các giá trị được lập chỉ mục.

Nhập khẩu


require 'vendor/autoload.php';

use Aws\SecretsManager\SecretsManagerClient; 
use Aws\Exception\AwsException;

Mã mẫu

$client = new SecretsManagerClient([
    'profile' => 'default',
    'version' => '2017-10-17',
    'region' => 'us-west-2'
]);

$secretName = '<<{{MySecretName}}>>';
$secret = '{"username":"<<USERNAME>>","password":"<<PASSWORD>>"}';

try {
    $result = $client->putSecretValue([
        'SecretId' => $secretName,
        'SecretString' => $secret,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
 

Lấy bí mật từ người quản lý bí mật

Để lấy lại giá trị của một bí mật được lưu trữ trong Trình quản lý bí mật, hãy sử dụng thao tác GetSecretValue.

Trong ví dụ này, bí mật là một chuỗi chứa giá trị được lưu trữ. Nếu được gọi vào bí mật chúng tôi đã tạo trước đó, mẫu này sẽ xuất ra

$client = new SecretsManagerClient([
    'profile' => 'default',
    'version' => '2017-10-17',
    'region' => 'us-west-2'
]);

$secretName = '<<{{MySecretName}}>>';
$secret = '{"username":"<<USERNAME>>","password":"<<PASSWORD>>"}';
$description = '<<Description>>';

try {
    $result = $client->createSecret([
        'Description' => $description,
        'Name' => $secretName,
        'SecretString' => $secret,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
 
6. Sử dụng
$client = new SecretsManagerClient([
    'profile' => 'default',
    'version' => '2017-10-17',
    'region' => 'us-west-2'
]);

$secretName = '<<{{MySecretName}}>>';
$secret = '{"username":"<<USERNAME>>","password":"<<PASSWORD>>"}';
$description = '<<Description>>';

try {
    $result = $client->createSecret([
        'Description' => $description,
        'Name' => $secretName,
        'SecretString' => $secret,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
 
7 để truy cập các giá trị được lập chỉ mục.

Liệt kê các bí mật được lưu trữ trong Trình quản lý bí mật

Nhập khẩu


require 'vendor/autoload.php';

use Aws\SecretsManager\SecretsManagerClient; 
use Aws\Exception\AwsException;

Mã mẫu

$client = new SecretsManagerClient([
    'profile' => 'default',
    'version' => '2017-10-17',
    'region' => 'us-west-2'
]);

$secretName = '<<{{MySecretName}}>>';
$secret = '{"username":"<<USERNAME>>","password":"<<PASSWORD>>"}';
$description = '<<Description>>';

try {
    $result = $client->createSecret([
        'Description' => $description,
        'Name' => $secretName,
        'SecretString' => $secret,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
 
1

Lấy bí mật từ người quản lý bí mật

Nhập khẩu


require 'vendor/autoload.php';

use Aws\SecretsManager\SecretsManagerClient; 
use Aws\Exception\AwsException;

Mã mẫu

$client = new SecretsManagerClient([
    'profile' => 'default',
    'version' => '2017-10-17',
    'region' => 'us-west-2'
]);

$secretName = '<<{{MySecretName}}>>';
$secret = '{"username":"<<USERNAME>>","password":"<<PASSWORD>>"}';
$description = '<<Description>>';

try {
    $result = $client->createSecret([
        'Description' => $description,
        'Name' => $secretName,
        'SecretString' => $secret,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
 
3

Lấy bí mật từ người quản lý bí mật

Để lấy lại giá trị của một bí mật được lưu trữ trong Trình quản lý bí mật, hãy sử dụng thao tác GetSecretValue.

Nhập khẩu


require 'vendor/autoload.php';

use Aws\SecretsManager\SecretsManagerClient; 
use Aws\Exception\AwsException;

Mã mẫu

$client = new SecretsManagerClient([
    'profile' => 'default',
    'version' => '2017-10-17',
    'region' => 'us-west-2'
]);

$secretName = '<<{{MySecretName}}>>';
$secret = '{"username":"<<USERNAME>>","password":"<<PASSWORD>>"}';
$description = '<<Description>>';

try {
    $result = $client->createSecret([
        'Description' => $description,
        'Name' => $secretName,
        'SecretString' => $secret,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
 
5

Lấy bí mật từ người quản lý bí mật

  • Để lấy lại giá trị của một bí mật được lưu trữ trong Trình quản lý bí mật, hãy sử dụng thao tác GetSecretValue.

  • Trong ví dụ này, bí mật là một chuỗi chứa giá trị được lưu trữ. Nếu được gọi vào bí mật chúng tôi đã tạo trước đó, mẫu này sẽ xuất ra

    $client = new SecretsManagerClient([
        'profile' => 'default',
        'version' => '2017-10-17',
        'region' => 'us-west-2'
    ]);
    
    $secretName = '<<{{MySecretName}}>>';
    $secret = '{"username":"<<USERNAME>>","password":"<<PASSWORD>>"}';
    $description = '<<Description>>';
    
    try {
        $result = $client->createSecret([
            'Description' => $description,
            'Name' => $secretName,
            'SecretString' => $secret,
        ]);
        var_dump($result);
    } catch (AwsException $e) {
        // output error message if fails
        echo $e->getMessage();
        echo "\n";
    }
     
    
    6. Sử dụng
    $client = new SecretsManagerClient([
        'profile' => 'default',
        'version' => '2017-10-17',
        'region' => 'us-west-2'
    ]);
    
    $secretName = '<<{{MySecretName}}>>';
    $secret = '{"username":"<<USERNAME>>","password":"<<PASSWORD>>"}';
    $description = '<<Description>>';
    
    try {
        $result = $client->createSecret([
            'Description' => $description,
            'Name' => $secretName,
            'SecretString' => $secret,
        ]);
        var_dump($result);
    } catch (AwsException $e) {
        // output error message if fails
        echo $e->getMessage();
        echo "\n";
    }
     
    
    7 để truy cập các giá trị được lập chỉ mục.

  • Liệt kê các bí mật được lưu trữ trong Trình quản lý bí mật

  • Nhận danh sách tất cả các bí mật được lưu trữ bởi Trình quản lý Bí mật bằng cách sử dụng hoạt động của Danh sách.

  • Lấy chi tiết về một bí mật

  • Các bí mật được lưu trữ chứa siêu dữ liệu về các quy tắc xoay, khi nó được truy cập hoặc thay đổi lần cuối, các thẻ do người dùng tạo và tên tài nguyên Amazon (ARN). Để có được chi tiết của một bí mật được chỉ định được lưu trữ trong Trình quản lý Bí mật, hãy sử dụng hoạt động mô tả.

  • Cập nhật giá trị bí mật

Để lưu trữ một giá trị bí mật được mã hóa mới trong Trình quản lý bí mật, hãy sử dụng hoạt động PutSecretValue.