هذه الصفحة غير متوفرة حاليًا بلغتك المحلية. نحن نعمل جاهدين على إضافة المزيد من اللغات. شاكرين تفهمك ودعمك المستمر لنا.

Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

Rotating a Secret for Two Users

Updated on 2024-08-30 GMT+08:00

Overview

You can update the information of two users in a secret.

For example, if your application requires high availability and you perform single-user rotation, you may fail to access the application when changing the user password and updating the secret content. In this case, you need to use the multi-user secret rotation policy.

You must have an account, for example, Admin, that has the permission to create and change the passwords of user1 and user2. First, create a secret, create and save the account and password of user1, and record them as user1/password1. Then, create user2 by adding secret version v2, and save the password of user2 as user2/password2. If you change the password of user1, you need to add secret version v3 and record it as user1/password3. When the secret version v3 is being created, the application uses the existing secret version v2 to obtain information. Once v3 is ready, the temporary storage tag will be changed, and the application can use v3 to obtain information.

Constraints

Ensure your account has the KMS Administrator or KMS CMKFullAccess permission. For details, see DEW Permissions Management.

Secret Rotation APIs

You can call the following APIs to rotate secrets locally.

API

Description

Creating a Secret Version

Create a secret version.

Querying the Secret Version and Value

Query the information about a specified secret version and the plaintext secret value in the version.

Updating the Version Status of a Secret

Update the version status of a secret.

Querying the Status of a Secret Version

Query the status of a specified secret version.

Example of Dual-Account Secret Rotation

  1. Create a secret on the HUAWEI CLOUD console as the administrator. For details, see Creating a Secret.
  2. Prepare basic authentication information.
    • ACCESS_KEY: Access key of the Huawei account
    • SECRET_ACCESS_KEY: Secret access key of the Huawei account
    • PROJECT_ID: project ID of a HUAWEI CLOUD site. For details, see Project.
    • CSMS_ENDPOINT: endpoint for accessing CSMS. For details, see Endpoints.
    • There will be security risks if the AK/SK used for authentication is directly written into code. Encrypt the AK/SK in the configuration file or environment variables for storage.
    • In this example, the AK/SK stored in the environment variables are used for identity authentication. Configure the environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK in the local environment first.
  3. Rotate the secret for two users.

    In the example code,

    • secretName indicates the name of the secret created on the Huawei Cloud console.
    • secretString indicates the value saved in the secret created on the Huawei Cloud console.
    • versionId indicates the secret ID automatically generated after a secret is created on the Huawei Cloud console.
    • LATEST_VERSION indicates the secret version.
      import com.huaweicloud.sdk.core.auth.BasicCredentials;
      import com.huaweicloud.sdk.csms.v1.CsmsClient;
      import com.huaweicloud.sdk.csms.v1.model.CreateSecretVersionRequest;
      import com.huaweicloud.sdk.csms.v1.model.CreateSecretVersionRequestBody;
      import com.huaweicloud.sdk.csms.v1.model.CreateSecretVersionResponse;
      import com.huaweicloud.sdk.csms.v1.model.ListSecretStageRequest;
      import com.huaweicloud.sdk.csms.v1.model.ListSecretStageResponse;
      import com.huaweicloud.sdk.csms.v1.model.ShowSecretVersionRequest;
      import com.huaweicloud.sdk.csms.v1.model.ShowSecretVersionResponse;
      import com.huaweicloud.sdk.csms.v1.model.UpdateSecretStageRequest;
      import com.huaweicloud.sdk.csms.v1.model.UpdateSecretStageRequestBody;
      
      import java.util.Collections;
      import java.util.List;
      
      public class CsmsDualAccountExample {
          /**
           * Basic authentication information:
           * - ACCESS_KEY: Access key of the Huawei account
           * - SECRET_ACCESS_KEY: Secret access key of the Huawei account
           * - PROJECT_ID: Huawei Cloud project ID. For details, see https://support.huaweicloud.com/intl/en-us/productdesc-iam/iam_01_0023.html
      * - CSMS_ENDPOINT: endpoint address for accessing CSMS. For details, see https://support.huaweicloud.com/intl/en-us/api-dew/dew_02_0052.html.
           * - There will be security risks if the AK/SK used for authentication is directly written into code. Encrypt the AK/SK in the configuration file or environment variables for storage;
           * - In this example, the AK/SK stored in the environment variables are used for identity authentication. Configure the environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK in the local environment first.
           */
          private static final String ACCESS_KEY = System.getenv("HUAWEICLOUD_SDK_AK");
          private static final String SECRET_ACCESS_KEY = System.getenv("HUAWEICLOUD_SDK_SK");
          private static final String PROJECT_ID = "<ProjectID>";
          private static final String CSMS_ENDPOINT = "<CsmsEndpoint>";
      
          //Version ID used to query the latest secret version details.
          private static final String LATEST_VERSION = "latest";
          private static final String HW_CURRENT_STAGE = "SYSCURRENT";
          private static final String HW_PENDING_STAGE = "HW_PENDING";
      
          public static void main(String[] args) {
              String secretName = args[0];
              String secretString = args[1];
      
              dualAccountRotation(secretName, secretString);
          }
      
          /**
           * Example: secret rotation for two accounts
           *
           * @param secretName
           * @param secretString
           */
          private static void dualAccountRotation(String secretName, String secretString) {
              // Create a new secret version with a custom version status. Assume that the secret content is the account password of service A.
              CreateSecretVersionResponse newSecretVersion = createNewSecretVersionWithStage(secretName,
                      secretString, Collections.singletonList(HW_PENDING_STAGE));
              String versionId = newSecretVersion.getVersionMetadata().getId();
      
              // Before rotation, check whether the pending state is modified.
              ListSecretStageResponse listSecretStageResponse = showSecretStage(secretName, HW_PENDING_STAGE);
              assert listSecretStageResponse.getStage().getVersionId().equals(versionId);
      
              // After the account and password of service A are updated, the version status of the new secret is updated to SYSCURRENT. The old secret is still stored in the service and cannot be accessed by specifying latest.
              updateSecretStage(secretName, versionId, HW_CURRENT_STAGE);
      
              // Query the secret of the latest version by specifying latest to complete the dual-account secret rotation.
              ShowSecretVersionResponse secretResponse = showVersionDetail(secretName, LATEST_VERSION);
      
              assert secretResponse.getVersion().getSecretString().equals(secretString);
          }
      
          /**
           * Example of creating a secret
           * If you add a secret version by customizing the version status, the program will not point the SYSCURRENT version status to this version.
           *
           * @param secretName
           * @param newSecretVersionText
           * @param stageList
           * @return
           */
          private static CreateSecretVersionResponse createNewSecretVersionWithStage(String secretName,
                                                                                     String newSecretVersionText,
                                                                                     List<String> stageList) {
              CsmsClient csmsClient = getCsmsClient();
      
              //Host the new secret in CSMS.
              CreateSecretVersionRequest createSecretVersionRequest = new CreateSecretVersionRequest()
                      .withSecretName(secretName)
                      .withBody(new CreateSecretVersionRequestBody()
                              .withSecretString(newSecretVersionText)
                              .withVersionStages(stageList));
      
              CreateSecretVersionResponse secretVersion = csmsClient.createSecretVersion(createSecretVersionRequest);
      
              System.out.printf("Created new version success, version id: %s", secretVersion.getVersionMetadata().getId());
      
              return secretVersion;
          }
      
          /**
           * Point the input version state to the specified secret version.
           * @param secretName
           * @param versionId
           * @param newStageName
           */
          private static void updateSecretStage(String secretName, String versionId, String newStageName) {
              UpdateSecretStageRequest updateSecretStageRequest = new UpdateSecretStageRequest().withSecretName(secretName)
                      .withStageName(newStageName).withBody(new UpdateSecretStageRequestBody().withVersionId(versionId));
      
              CsmsClient csmsClient = getCsmsClient();
      
              csmsClient.updateSecretStage(updateSecretStageRequest);
      
              System.out.printf("Version stage update success. version id:%s, new stage name:%s", versionId, newStageName);
          }
      
          /**
           * Query the secret of a specified version.
           * @param secretName
           * @param versionId
           * @return
           */
          private static ShowSecretVersionResponse showVersionDetail(String secretName, String versionId) {
              ShowSecretVersionRequest showSecretVersionRequest = new ShowSecretVersionRequest().withSecretName(secretName)
                      .withVersionId(versionId);
      
              CsmsClient csmsClient = getCsmsClient();
              ShowSecretVersionResponse secretDetail = csmsClient.showSecretVersion(showSecretVersionRequest);
      
              System.out.printf("Query latest version success. version id:%s",
       secretDetail.getVersion().getVersionMetadata().getId());
              return secretDetail;
          }
      
          /**
           * Query the status of a specified version.
           * @param secretName
           * @param stageName
           * @return
           */
          private static ListSecretStageResponse showSecretStage(String secretName, String stageName) {
              ShowSecretStageRequest showSecretStageRequest = new ShowSecretStageRequest()
                      .withSecretName(secretName).withStageName(stageName);
              CsmsClient csmsClient = getCsmsClient();
              return csmsClient.showSecretStage(showSecretStageRequest);
          }
      
          /**
           * Obtain the CSMS client.
           *
           * @return
           */
          private static CsmsClient getCsmsClient() {
              BasicCredentials auth = new BasicCredentials()
                      .withAk(ACCESS_KEY)
                      .withSk(SECRET_ACCESS_KEY)
                      .withProjectId(PROJECT_ID);
              return CsmsClient.newBuilder().withCredential(auth).withEndpoint(CSMS_ENDPOINT).build();
          }
      }

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback