KMS Module
설치 가이드
react-native-aws-kms 설치 및 AWS 환경 설정
설치 가이드
요구 사항
| 항목 | 최소 버전 |
|---|---|
| React Native | 0.73+ |
| Expo SDK | 50+ |
| iOS | 13.0+ |
| Android | API 26+ |
| AWS SDK | 모듈 내장 |
패키지 설치
npm install @boostbrothers/react-native-aws-kms
AWS 자격증명 준비
모듈을 초기화하려면 AWS 자격증명이 필요합니다. 다음 중 하나의 방법으로 자격증명을 획득합니다.
AWS Cognito Identity Pool을 사용하면 임시 자격증명을 안전하게 획득할 수 있습니다.
import {CognitoIdentityClient,GetCredentialsForIdentityCommand,GetIdCommand,} from '@aws-sdk/client-cognito-identity';const cognitoClient = new CognitoIdentityClient({ region: 'us-east-1' });async function getAwsCredentials() {const { IdentityId } = await cognitoClient.send(new GetIdCommand({IdentityPoolId: 'us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',}));const { Credentials } = await cognitoClient.send(new GetCredentialsForIdentityCommand({ IdentityId }));return {accessKey: Credentials.AccessKeyId,secretKey: Credentials.SecretKey,sessionToken: Credentials.SessionToken,};}
백엔드 서버에서 STS를 통해 임시 자격증명을 발급받아 앱에 전달합니다.
// 백엔드에서 발급한 임시 자격증명을 API로 수신const response = await fetch('https://api.example.com/auth/aws-credentials');const { accessKey, secretKey, sessionToken } = await response.json();
개발/테스트 환경에서만 사용합니다. 프로덕션에서는 절대 하드코딩하지 마세요.
// .env 파일에서 로드 (react-native-dotenv 사용)import {AWS_ACCESS_KEY,AWS_SECRET_KEY,AWS_SESSION_TOKEN,AWS_KMS_KEY_ID,} from '@env';
AWS 자격증명을 소스 코드나 번들에 포함하면 보안 위험이 있습니다. Cognito Identity Pool 또는 서버 사이드 자격증명 발급을 사용하세요.
AWS KMS 키 준비
AWS 콘솔에서 KMS 키를 생성하고 ARN을 확인합니다.
- AWS KMS 콘솔 접속
- Customer managed keys > Create key 선택
- Key type: Symmetric, Key usage: Encrypt and decrypt 선택
- 생성된 키의 ARN 복사 (예:
arn:aws:kms:ap-northeast-2:123456789012:key/mrk-xxxxxxxx)
Multi-Region Key(MRK)를 사용하면 여러 AWS 리전에서 동일한 키를 사용할 수 있습니다. 단일 리전 사용이라면 일반 키를 사용해도 충분합니다.
네이티브 빌드
npx expo prebuildnpx expo run:ios # iOSnpx expo run:android # Android
# iOScd ios && pod install && cd ..npx react-native run-ios# Androidnpx react-native run-android
설치 확인
import AwsKmsExpoModule from '@boostbrothers/react-native-aws-kms';async function verifyInstallation() {try {const result = await AwsKmsExpoModule.init({accessKey: 'YOUR_ACCESS_KEY',secretKey: 'YOUR_SECRET_KEY',sessionToken: 'YOUR_SESSION_TOKEN',keyId: 'YOUR_KMS_KEY_ARN',});console.log('KMS 초기화 성공:', result);} catch (error) {console.error('KMS 초기화 실패:', error);}}
IAM 권한 설정
KMS 키를 사용하는 IAM 역할 또는 사용자에게 다음 권한이 필요합니다.
{"Version": "2012-10-17","Statement": [{"Effect": "Allow","Action": ["kms:GenerateDataKey","kms:Decrypt","kms:Encrypt","kms:DescribeKey"],"Resource": "arn:aws:kms:ap-northeast-2:123456789012:key/YOUR_KEY_ID"}]}
Android 네트워크 설정
Android에서 AWS API 호출을 허용하려면 android/app/src/main/AndroidManifest.xml에 인터넷 권한이 있는지 확인합니다.
<uses-permission android:name="android.permission.INTERNET" />