File tree Expand file tree Collapse file tree 2 files changed +52
-1
lines changed
src/Illuminate/Foundation/Console
tests/Integration/Console Expand file tree Collapse file tree 2 files changed +52
-1
lines changed Original file line number Diff line number Diff line change 6
6
use Illuminate \Console \Command ;
7
7
use Illuminate \Encryption \Encrypter ;
8
8
use Illuminate \Filesystem \Filesystem ;
9
+ use Illuminate \Support \Str ;
9
10
use Symfony \Component \Console \Attribute \AsCommand ;
10
11
11
12
#[AsCommand(name: 'env:encrypt ' )]
@@ -96,7 +97,7 @@ public function handle()
96
97
}
97
98
98
99
try {
99
- $ encrypter = new Encrypter ($ key , $ cipher );
100
+ $ encrypter = new Encrypter ($ this -> parseKey ( $ key) , $ cipher );
100
101
101
102
$ this ->files ->put (
102
103
$ encryptedFile ,
@@ -116,4 +117,19 @@ public function handle()
116
117
117
118
$ this ->newLine ();
118
119
}
120
+
121
+ /**
122
+ * Parse the encryption key.
123
+ *
124
+ * @param string $key
125
+ * @return string
126
+ */
127
+ protected function parseKey (string $ key )
128
+ {
129
+ if (Str::startsWith ($ key , $ prefix = 'base64: ' )) {
130
+ $ key = base64_decode (Str::after ($ key , $ prefix ));
131
+ }
132
+
133
+ return $ key ;
134
+ }
119
135
}
Original file line number Diff line number Diff line change 2
2
3
3
namespace Illuminate \Tests \Integration \Console ;
4
4
5
+ use Illuminate \Encryption \Encrypter ;
5
6
use Illuminate \Filesystem \Filesystem ;
6
7
use Illuminate \Support \Facades \File ;
7
8
use Mockery as m ;
@@ -120,4 +121,38 @@ public function testItGeneratesTheEncryptionFileWhenForcing()
120
121
$ this ->filesystem ->shouldHaveReceived ('put ' )
121
122
->with (base_path ('.env.encrypted ' ), m::any ());
122
123
}
124
+
125
+ public function testItEncryptsWithGivenKeyAndDisplaysIt ()
126
+ {
127
+ $ this ->filesystem ->shouldReceive ('exists ' )
128
+ ->once ()
129
+ ->andReturn (true )
130
+ ->shouldReceive ('exists ' )
131
+ ->once ()
132
+ ->andReturn (false );
133
+
134
+ $ this ->artisan ('env:encrypt ' , ['--key ' => $ key = 'ANvVbPbE0tWMHpUySh6liY4WaCmAYKXP ' ])
135
+ ->expectsOutputToContain ('Environment successfully encrypted ' )
136
+ ->expectsOutputToContain ($ key )
137
+ ->expectsOutputToContain ('.env.encrypted ' )
138
+ ->assertExitCode (0 );
139
+ }
140
+
141
+ public function testItEncryptsWithGivenGeneratedBase64KeyAndDisplaysIt ()
142
+ {
143
+ $ this ->filesystem ->shouldReceive ('exists ' )
144
+ ->once ()
145
+ ->andReturn (true )
146
+ ->shouldReceive ('exists ' )
147
+ ->once ()
148
+ ->andReturn (false );
149
+
150
+ $ key = Encrypter::generateKey ('AES-256-CBC ' );
151
+
152
+ $ this ->artisan ('env:encrypt ' , ['--key ' => 'base64: ' .base64_encode ($ key )])
153
+ ->expectsOutputToContain ('Environment successfully encrypted ' )
154
+ ->expectsOutputToContain ('base64: ' .base64_encode ($ key ))
155
+ ->expectsOutputToContain ('.env.encrypted ' )
156
+ ->assertExitCode (0 );
157
+ }
123
158
}
You can’t perform that action at this time.
0 commit comments