æè¡1課ã®æ°´æ¬ã§ãã
ã¢ããªã±ã¼ã·ã§ã³éçºã«ããã¦ãã·ã¼ã¯ã¬ããï¼ç§å¯æ å ±ï¼ã®åã渡ãã¯èª°ãã課é¡ã«ããã¨ããã§ã¯ãªãã§ããããã
- dotenvãã¡ã¤ã«(Nuxtãªã©)
- database.yml / master.keyï¼Railsï¼
DevOpsã¨ããã¯ã¼ããé£ã³äº¤ã£ã¦ããä»ã§ããã¤ã³ãã©æ å½ã¯ããªãã¹ãã¢ããªãã¼ã ã®ä½¿ãåæãå¤ããªãã¾ã¾ãå®å ¨ã«ç®¡çããæ¹æ³ããèãã¦ããããã¨æãã¾ãã
ä»åã¯ãã®æ¹æ³ã®ããªã¨ã¼ã·ã§ã³ã®ã²ã¨ã¤ã¨ãã¦ããSOPSããç¨ããæå·åã«ã¤ãã¦ç´¹ä»ãã¾ãã
SOPSã¨ã¯ï¼
SOPSï¼Secrets OPerationSï¼ã¨ã¯Mozillaï¼Firefoxãªã©ã®éçºå ï¼ãå ¬éãã¦ããæå·åãã¼ã«ã§ãã
æå·åãã¼ã«ã¨ããã®ã¯ä»ã«ãåå¨ãã¾ãããSOPSã¯PGPéµã§ã®æå·åæ©è½ã®ä»ã以ä¸ã®ç¹é·ãæã¡ã¾ãã
- 主è¦ã¯ã©ã¦ãï¼AWSãGoogle CloudãAzureï¼ã®éµãµã¼ãã¹ã§çºè¡ããéµãç¨ãã¦ã®æå·åã«å¯¾å¿
- ãã¡ã¤ã«ã®ç¨®é¡ãå¤å¥ãã¦å¤ã ããæå·åæ¸å¤ã«å·®ãæ¿ããã®ã§ãæå·åæ¸ã¿ãã¡ã¤ã«ã§ãå 容ãèªããï¼å¯¾å¿ãã¡ã¤ã«ï¼YAML/JSON/Dotenv/INIï¼
ä½ã便å©ãªã®ãï¼
ãAWSãªãSSMãã©ã¡ã¼ã¿ã¼ã¹ãã¢ã¿ããã«ãå種ã¯ã©ã¦ãã«ãã·ã¼ã¯ã¬ããã®å°ç¨ç®¡çãµã¼ãã¹ãããã®ã§ã¯ï¼ãã¨æãããã§ãããã
ããããã¢ããªéçºãã¼ã ãå¿ ãããAWSã®æä½ã«é·ãã¦ããããã§ã¯ãªããããªãã¹ãAWSã¯è§¦ããã«æ¥½ã«éçºããããã¨ã®è¦æãåºãã¨ãã«èããããæ段ã¯ãã·ã¼ã¯ã¬ããã¯ãã¡ã¤ã«åããGitã®pushã«é£åãã¦æå·åãããã ã¨èãã¾ãã
ä¾ãã°ã以ä¸ã®æ¡ä»¶ã®ç°å¢ãããã¨ãã¾ãããã
- ã¢ããªã®ã·ã¼ã¯ã¬ããã
.env
ãã¡ã¤ã«ã«æ ¼ç´ãã¦ãã .env
ã«ã¯DBèªè¨¼æ å ±ãæ¸ããã¦ãããGitHubã¸ã®ã¢ããã¯ç¦æ¢
ãã®å ´åãä¸è¨ã®éãã«èªååãããã¨ã§ãå®å ¨ã«ã·ã¼ã¯ã¬ããã®åã渡ããå¯è½ã§ãããã¢ããªéçºãã¼ã ã¯ç¹ã«AWSãæä½ãããå®å ¨ã«ã·ã¼ã¯ã¬ãããã¤ã³ãã©ã«åã渡ããã¨ãå¯è½ã«ãªãã¾ãã
- éå ¬éãªS3ãä½æããï¼ã·ã¼ã¯ã¬ãã管çç¨ï¼
- ã¢ããªã³ã¼ããæ ¼ç´ããGitHubãªãã¸ããªã«ã
.env
ã.enc.env
ã«ãªãã¼ã ããå½¢ã§æå·åãã¦S3ã¸ã¢ãããã¼ãããCIãè¨å® - 2ãããªã¬ã¼ã¨ãã¦
.enc.env
ã¨ã¢ããªã³ã¼ããå ¥æããå¾ç¶ã¸ã§ããã¹ã¿ã¼ã .enc.env
復å·ãã¦.env
ã«- 4ãã¢ããªã³ã¼ãã«ãã¼ã¸ãã¦ã¤ã³ãã©ã«ãããã¤
å®éã«æå·åãã¦ã¿ã
ã§ã¯å®éã«æå·åããã¦ã¿ã¾ãã
æºå
ä¸è¨ãå¿ è¦ã§ãã®ã§äºåã«æºåãã ããã
- SOPSãã¤ã³ã¹ãã¼ã«æ¸
- AWS CLIã»ããã¢ããæ¸ã¿ã§ãæ£ãããããã¡ã¤ã«ã®è¨å®ã¾ã§å®äºãã¦ãã
- 1ããæå·å復å·åã§ããKMSéµãçºè¡æ¸ã¿ï¼ARNãæ§ãã¦ãã ããï¼
æå·å
ã§ã¯ãä¸è¨ã®ãã¡ã¤ã«.env
ãæå·åãã¦ã¿ã¾ãããã
ãã®ãã¡ã¤ã«ã¯é©å½ã«ä½ã£ããã®ã§ãã
DBHOSTNAME="mygreatdbhost" DBUSERNAME="mydbmasteruser" DBPASSWORD="mydbmasterpassword"
DBæ
å ±ããã®ã¾ã¾å
¥ã£ã¦ãã¾ã£ã¦ã¾ããå±éºã§ããã
ï¼æ¬æ¥ã¯ãããªãã¨ã¯ç¡ãã¨æãã¾ããï¼
ã§ã¯ããã®ãã¡ã¤ã«ã«å¯¾ãã¦æå·åãè¡ã£ã¦ã¿ã¾ãã
æ¹æ³ã¯ã·ã³ãã«ã§ã
sops -e --kms ï¼KMSã®ARNï¼ ï¼--aws-profile AWS CLIã®ãããã¡ã¤ã«åãçç¥å¯è½ï¼ ï¼ãã¡ã¤ã«åï¼ > ï¼æå·åå¾ãã¡ã¤ã«åï¼
ã§ãã
$ sops -e --kms arn:aws:kms:ap-northeast-1:123412341234:alias/sops-demo --aws-profile swx-labo .env > .enc.env
æå·åãã¡ã¤ã«.enc.env
ã¯ãããªæãã«ãªãã¾ãã
DBHOSTNAME=ENC[AES256_GCM,data:QBQJ3t3reViojLI7NMSZ,iv:x5NcyhKlWfj34g031ByWwi/eFKfjCs3WZMSmtfOGRrg=,tag:DZ+wuOaJm33xt7RUm5UQjA==,type:str] DBUSERNAME=ENC[AES256_GCM,data:wpuYMykNW3c7AEBy1dvgnA==,iv:s3JJ99TShKOY1XdGvNuAy8b5DYSFsEppq9ayrCBhlbQ=,tag:Cr8+/IK67KO2ZLY5B66rFg==,type:str] DBPASSWORD=ENC[AES256_GCM,data:EA7pw5sObLqNJIl/4t47j41SAEQ=,iv:uQEhScWwXgF8JipQ8iL759GYbo5bAoB4dZ+NKobHhNM=,tag:/A/XPCoJ6g4bHwy29LMxhg==,type:str] sops_unencrypted_suffix=_unencrypted sops_version=3.7.1 sops_mac=ENC[AES256_GCM,data:j3XkbxFu1nPtKbZCwiGPOC7VpZxBqM5+wafkP57+YLbhT8AksbgHntC2CQDhF9O+b3aAzUKJf1t4WNBXMM/YA7Koy77kKaHu+tex7JzG/DUNGTQXsUZrqQXUc3A2yAkj24hHAAM4B3tk4zC9Qah81Abymla9s2kYnpXuD0lPyOU=,iv:xqL466FIwlHDfHhapgmFmpyBjnTo9O/CJR7IBB4VFeI=,tag:GZZcB78ARVSFS1MlPHGgRg==,type:str] sops_lastmodified=2021-10-21T06:52:49Z sops_kms__list_0__map_arn=arn:aws:kms:ap-northeast-1:123412341234:alias/sops-demo sops_kms__list_0__map_created_at=2021-10-21T06:52:49Z sops_kms__list_0__map_enc=AQICAHhaAtv1/ZjvWfP/a+ng4YhCpQSY/iMr6eOwRzlD9G6QSAE0G9HESBSt8x/bK1zG4ClHAAAAfjB8BgkqhkiG9w0BBwagbzBtAgEAMGgGCSqGSIb3DQEHATAeBglghkgBZQMEAS4wEQQM1D+VhC8Lau1mKFV4AgEQgDu19BtTezYjIUYY9oCzp9buFaiz8XNMtoe/zRxkllxewBwDw/C9MQoYIAQyg/FU+J126iSFTJSVHHSMTw== sops_kms__list_0__map_aws_profile=swx-labo
æå·ååã¨æ¯ã¹ã¦å¤åãããã¾ããã
- ãã¼åã¯ãã®ã¾ã¾ã«ãå¤ãæå·åããã¦ãã
sops_
ã§å§ã¾ããã¼ã追å ããã¦ãã- æå·å管çã»å¾©å·åã«ä½¿ãã¡ã¿ãã¼ã¿
DBHOSTNAME=ENC[AES256_GCM,data:QBQJ3t3reViojLI7NMSZ,iv:x5NcyhKlWfj34g031ByWwi/eFKfjCs3WZMSmtfOGRrg=,tag:DZ+wuOaJm33xt7RUm5UQjA==,type:str] DBUSERNAME=ENC[AES256_GCM,data:wpuYMykNW3c7AEBy1dvgnA==,iv:s3JJ99TShKOY1XdGvNuAy8b5DYSFsEppq9ayrCBhlbQ=,tag:Cr8+/IK67KO2ZLY5B66rFg==,type:str] DBPASSWORD=ENC[AES256_GCM,data:EA7pw5sObLqNJIl/4t47j41SAEQ=,iv:uQEhScWwXgF8JipQ8iL759GYbo5bAoB4dZ+NKobHhNM=,tag:/A/XPCoJ6g4bHwy29LMxhg==,type:str]
ãã£ããDBæ¥ç¶æ å ±ã¯åãããªããªã£ã¦ãã¾ããã
復å·
復å·ã®éã¯éµæ
å ±ãã¡ã¿ãã¼ã¿ããåå¾ããã®ã§ãç¹ã«æ示ã¯ä¸è¦ã§ãã
sops -d .enc.env > .env
ãªããæå·åãã¡ã¤ã«ãç´æ¥ç·¨éãããã¨ãå¯è½ã§ãã
$EDITOR
ã®ã¨ãã£ã¿ãéããã¾ãã
sops .enc.env
注æäºé
æåã®ä¾ã«æãã¦ããã¦ãªããªã®ã§ããã試ãã«Railsããä½ã£ãdatabase.ymlãæå·åãã¦å¾©å·ããã¨ãããã©ããã<<: *default
ã®è¡¨è¨ãæ£ãã解éã§ããªããããªã®ã§ããå¯èªæ§ããªããªã£ã¦ãè¯ãããæå·åãããï¼ãã¨ããå ´åã¯ã--input-type binary
ãä»ä¸ãã¦æå·åããã»ããè¯ãããã§ããï¼ç§ã¯SOPSã«æ¸¡ãåã«base64ãçµç±ããããã¨ã§å¯¾å¿ãã¾ãããï¼
以ä¸ãSOPSãç¨ããç§å¯æ å ±ãã¡ã¤ã«ç®¡çæ¹æ³ã®ç´¹ä»ã§ããã
æ°´æ¬ æ£æ(å·çè¨äºã®ä¸è¦§)
ã¨ã³ã¿ã¼ãã©ã¤ãºã¯ã©ã¦ãé¨ ã½ãªã¥ã¼ã·ã§ã³ã¢ã¼ããã¯ã1課
å½å ITãã³ãã¼ã®ã«ã¹ã¿ãã¼ã¨ã³ã¸ãã¢ããAWSã«é äºãããµã¼ãã¼ã¯ã¼ã¯ã¹ã¸ã