หลุมฝังศพที่ใช้ PHP ที่ยืดหยุ่นเพื่อให้ความลับแบบไดนามิก
โครงการนี้ได้รับใบอนุญาตภายใต้ GNU LGPL 3.0
composer install technicalguru/vaultคุณสามารถดาวน์โหลดแพ็คเกจซอร์สโค้ดได้จากหน้า GitHub
ขั้นตอนนี้อธิบายได้ดีที่สุดที่บล็อก Hashicorp มันอธิบายวิธีการสร้าง approle นี่คือสาระสำคัญของมัน:
# Enable the auth method for approle
vault auth enable approle
# Create a renewal policy
echo ' path "auth/token/*" { capabilities = [ "create", "read", "update", "delete", "list", "sudo" ] } ' > renewal-policy.hcl
vault policy write renewal-policy renewal-policy.hcl
# Create a file with your policy on the respective secret path:
cat ' path "secret/my-secret" { capabilities = ["read", "list"] } ' > app-policy.hcl
# Create the policy
vault policy write my-app-policy app-policy.hcl
# Create the approle with renewal-policy and your application policy
vault write auth/approle/role/my-approle token_policies=renewal-policy,my-app-policy token_period=30m token_ttl=30m token_max_ttl=1h token_explicit_max_ttl=2h
# Get the role ID printed
vault read auth/approle/role/my-approle/role-id
# Create the secret ID and print it
vault write -f auth/approle/role/my-approle/secret-idโปรดสังเกตว่าคุณต้องสร้างรหัสลับขึ้นใหม่เมื่อใดก็ตามที่คุณเปลี่ยนบทบาทแอปพลิเคชันหรือนโยบาย
โปรดทราบว่าห้องนิรภัยนี้เป็นลูกค้าของ Hashicorp Vault ที่มีอยู่
// Create configuration
$ config = array (
' type ' => ' hashicorp ' ,
' config ' => array (
' uri ' => ' https://127.0.0.1:8200/v1 ' ,
' roleId ' => ' 123456-12345-12345-123456 ' ,
' secretId ' => ' abcdef-abcde-abcde-abcdef '
)
);
// Create the vault instance
try {
$ vault = TgVault VaultFactory:: create ( $ config );
} catch ( TgVault VaultException $ e ) {
// Vault could not be created
} // Create configuration
$ config = array (
' type ' => ' memory ' ,
' config ' => array (
' secrets ' => array (
' my/secret/number/1 ' => array (
' username ' => ' my-username1 ' ,
' password ' => ' my-password1 ' ,
),
' my/secret/number/2 ' => array (
' username ' => ' my-username2 ' ,
' password ' => ' my-password2 ' ,
),
)
)
);
// Create the vault instance
try {
$ vault = TgVault VaultFactory:: create ( $ config );
} catch ( TgVault VaultException $ e ) {
// Vault could not be created
} // Create configuration
$ config = array (
' type ' => ' file ' ,
' config ' => array (
' filename ' => ' path-to-json-secret-file '
)
);
// Create the vault instance
try {
$ vault = TgVault VaultFactory:: create ( $ config );
} catch ( TgVault VaultException $ e ) {
// Vault could not be created
}ไฟล์ความลับ (JSON) จะมีลักษณะเช่นนี้:
{
"secrets" : {
"my/secret/number/1" : {
"username" : " my-username1 " ,
"password" : " my-password1 "
},
"my/secret/number/2" : {
"username" : " my-username2 " ,
"password" : " my-password2 "
}
}
} try {
$ mySecret1 = $ vault -> getSecret ( ' my/secret/number/1 ' );
$ mySecret2 = $ vault -> getSecret ( ' my/secret/number/2 ' );
} catch ( TgVault VaultException $ e ) {
// secret was not found
}
$ username1 = $ mySecret1 -> get ( ' username ' );
$ password1 = $ mySecret1 -> get ( ' password ' );
$ username2 = $ mySecret2 -> get ( ' username ' );
$ password2 = $ mySecret2 -> get ( ' password ' ); ค่าในความลับนั้นเป็น NULL เมื่อคีย์ไม่มีอยู่ในขณะที่ข้อยกเว้นจะถูกโยนลงเมื่อไม่พบความลับหรือเกิดข้อผิดพลาดในขณะที่ดึงข้อมูล
คุณสามารถใช้คลาส SecretProvider หรือ CredentialsProvider Helper เพื่อส่งผ่านข้อมูลประจำตัวโดยไม่ทราบว่าพวกเขามาจากไหนหรือใช้ห้องนิรภัย
$ callback1 = new TgVault SecretProvider ( $ vault , ' my/secret/number/1 ' );
$ callback2 = new TgVault CredentialsProvider ( $ vault , ' my/secret/number/2 ' );
try {
$ username1 = $ callback1 -> get ( ' username ' );
$ password1 = $ callback1 -> get ( ' password ' );
$ username2 = $ callback2 -> getUsername ();
$ password2 = $ callback2 -> getPassword ();
} catch ( TgVault VaultException $ e ) {
// Secret cannot be retrieved or does not exist
} CredentialsProvider ใช้อาร์กิวเมนต์ตัวสร้างเพิ่มเติมที่กำหนดซึ่งคีย์ในความลับมีชื่อผู้ใช้และรหัสผ่าน ค่าเริ่มต้นตามที่ระบุไว้ข้างต้นสำหรับ SecretProvider
รายงานข้อผิดพลาดขอการปรับปรุงหรือคำขอดึงที่ตัวติดตามปัญหา GitHub