-
Notifications
You must be signed in to change notification settings - Fork 396
/
SimpleSamlPhpCommand.php
247 lines (219 loc) · 9.08 KB
/
SimpleSamlPhpCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
namespace Acquia\Blt\Robo\Commands\Saml;
use Acquia\Blt\Robo\BltTasks;
use Acquia\Blt\Robo\Common\YamlMunge;
use Acquia\Blt\Robo\Exceptions\BltException;
use Robo\Contract\VerbosityThresholdInterface;
use Symfony\Component\Console\Helper\FormatterHelper;
/**
* Defines commands in the "simplesamlphp:*" namespace.
*/
class SimpleSamlPhpCommand extends BltTasks {
protected $bltRoot;
protected $repoRoot;
protected $deployDir;
/**
* @var \Symfony\Component\Console\Helper\FormatterHelper
*/
protected $formatter;
/**
* This hook will fire for all commands in this command file.
*
* @hook init
*/
public function initialize() {
$this->bltRoot = $this->getConfigValue('blt.root');
$this->repoRoot = $this->getConfigValue('repo.root');
$this->deployDir = $this->getConfigValue('deploy.dir');
$this->formatter = new FormatterHelper();
}
/**
* Initializes SimpleSAMLphp for project.
*
* @command recipes:simplesamlphp:init
* @aliases rsi saml simplesamlphp:init
*/
public function initializeSimpleSamlPhp() {
$this->requireModule();
$this->initializeConfig();
$this->setSimpleSamlPhpInstalled();
$this->symlinkDocrootToLibDir();
$this->addHtaccessPatch();
$this->outputCompleteSetupInstructions();
}
/**
* Adds simplesamlphp_auth as a dependency.
*
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
protected function requireModule() {
$this->say('Adding SimpleSAMLphp Auth module as a dependency...');
$package_options = [
'package_name' => 'drupal/simplesamlphp_auth',
'package_version' => '^3.0',
];
$this->invokeCommand('internal:composer:require', $package_options);
}
/**
* Copies configuration templates from SimpleSamlPHP to the repo root.
*
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
protected function initializeConfig() {
$destinationDirectory = "{$this->repoRoot}/simplesamlphp/config";
$this->say("Copying config files to ${destinationDirectory}...");
$result = $this->taskFileSystemStack()
->copy("{$this->repoRoot}/vendor/simplesamlphp/simplesamlphp/config-templates/authsources.php", "${destinationDirectory}/authsources.php", TRUE)
->copy("{$this->repoRoot}/vendor/simplesamlphp/simplesamlphp/config-templates/config.php", "${destinationDirectory}/config.php", TRUE)
->copy("{$this->bltRoot}/scripts/simplesamlphp/acquia_config.php", "${destinationDirectory}/acquia_config.php", TRUE)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();
if (!$result->wasSuccessful()) {
throw new BltException("Unable to copy SimpleSamlPhp config files.");
}
$config_file = "{$this->repoRoot}/simplesamlphp/config/config.php";
$result = $this->taskWriteToFile($config_file)
->text("include 'acquia_config.php';")
->append()
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();
if (!$result->wasSuccessful()) {
throw new BltException("Unable modify $config_file.");
}
$this->say("Copying config files to {$this->repoRoot}/simplesamlphp/metadata...");
$result = $this->taskFileSystemStack()
->copy("{$this->repoRoot}/vendor/simplesamlphp/simplesamlphp/metadata-templates/saml20-idp-remote.php", "{$this->repoRoot}/simplesamlphp/metadata/saml20-idp-remote.php", TRUE)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();
if (!$result->wasSuccessful()) {
throw new BltException("Unable to initialize SimpleSamlPhp configuration.");
}
}
/**
* Copies custom config files to SimpleSamlPHP in deploy artifact.
*
* @command artifact:build:simplesamlphp-config
* @aliases absc
* @throws BltException
*/
public function simpleSamlPhpDeployConfig() {
$this->say('Copying config files to the appropriate place in simplesamlphp library in the deploy artifact...');
$result = $this->taskCopyDir(["{$this->repoRoot}/simplesamlphp" => "{$this->deployDir}/vendor/simplesamlphp/simplesamlphp"])
->overwrite(TRUE)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();
if (!$result->wasSuccessful()) {
throw new BltException("Unable to copy SimpleSamlPhp files into deployment artifact.");
}
$result = $this->taskFileSystemStack()
->copy("{$this->bltRoot}/scripts/simplesamlphp/gitignore.txt", "{$this->deployDir}/vendor/simplesamlphp/simplesamlphp/.gitignore", TRUE)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();
if (!$result->wasSuccessful()) {
throw new BltException("Unable to copy SimpleSamlPhp .gitignore into deployment artifact.");
}
}
/**
* Sets value in blt.yml to let targets know simplesamlphp is installed.
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
protected function setSimpleSamlPhpInstalled() {
$project_yml = $this->getConfigValue('blt.config-files.project');
$this->say("Updating ${project_yml}...");
$project_config = YamlMunge::parseFile($project_yml);
$project_config['simplesamlphp'] = TRUE;
try {
YamlMunge::writeFile($project_yml, $project_config);
}
catch (\Exception $e) {
throw new BltException("Unable to update $project_yml.");
}
}
/**
* Creates a symlink from the docroot to the web accessible library dir.
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
protected function symlinkDocrootToLibDir() {
$docroot = $this->getConfigValue('docroot');
$this->say("Creating a symbolic link from ${docroot}/simplesaml to web accessible directory in the simplesamlphp library...");
$result = $this->taskFileSystemStack()
->symlink("../vendor/simplesamlphp/simplesamlphp/www", "${docroot}/simplesaml")
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();
if (!$result->wasSuccessful()) {
throw new BltException("Unable create symlink.");
}
}
/**
* Copies customized config files into vendored SimpleSamlPHP.
*
* @command source:build:simplesamlphp-config
* @aliases sbsc
* @throws \Acquia\Blt\Robo\Exceptions\BltException
*/
public function simpleSamlPhpBuildConfig() {
$this->say('Copying config files to the appropriate place in simplesamlphp library...');
$result = $this->taskCopyDir(["{$this->repoRoot}/simplesamlphp" => "{$this->repoRoot}/vendor/simplesamlphp/simplesamlphp"])
->overwrite(TRUE)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();
if (!$result->wasSuccessful()) {
throw new BltException("Unable to copy configuration into SimpleSamlPhp.");
}
$result = $this->taskFileSystemStack()
->copy("{$this->bltRoot}/scripts/simplesamlphp/gitignore.txt", "{$this->repoRoot}/vendor/simplesamlphp/simplesamlphp/.gitignore", TRUE)
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_VERBOSE)
->run();
if (!$result->wasSuccessful()) {
throw new BltException("Unable to copy .gitignore into SimpleSamlPhp.");
}
}
/**
* Ensures SimpleSamlPhp enabled repos have config copied on composer runs.
*
* @hook post-command source:build:composer
*/
public function postComposerHook() {
if ($this->getConfig()->has('simplesamlphp') && $this->getConfigValue('simplesamlphp')) {
$this->invokeCommand('source:build:simplesamlphp-config');
}
}
/**
* Outputs a message to edit the new config files.
*/
protected function outputCompleteSetupInstructions() {
$instructions = [
'To complete the setup you must manually modify several files:',
'',
"* {$this->repoRoot}/simplesamlphp/config/acquia_config.php",
"* {$this->repoRoot}/simplesamlphp/config/authsources.php",
"* {$this->repoRoot}/simplesamlphp/metadata/saml20-idp-remote.php",
'',
'After editing these files execute the following command to copy the modified files to the correct location in the SimpleSAMLphp library:',
'',
"'blt source:build:simplesamlphp-config'",
'',
"See http://blt.readthedocs.io/en/latest/readme/simplesamlphp-setup/ for details on how to modify the files.",
];
$formattedBlock = $this->formatter->formatBlock($instructions, 'comment', TRUE);
$this->writeln($formattedBlock);
}
/**
* Add a patch to .htaccess.
*/
protected function addHtaccessPatch() {
$this->taskFilesystemStack()
->copy($this->bltRoot . "/scripts/simplesamlphp/htaccess-saml.patch",
$this->repoRoot . "/patches/htaccess-saml.patch")
->run();
$composer_json = json_decode(file_get_contents($this->getConfigValue('repo.root') . '/composer.json'));
$composer_json->scripts->{"post-drupal-scaffold-cmd"}[] = "cd docroot && patch -p1 <../patches/htaccess-saml.patch";
file_put_contents($this->getConfigValue('repo.root') . '/composer.json',
json_encode($composer_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$this->taskExecStack()
->dir($this->getConfigValue('repo.roou'))
->exec("composer post-drupal-scaffold-cmd")
->run();
// @todo throw exceptions.
}
}