-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
245 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...-gitlab/src/main/java/com/github/skapral/puzzler/gitlab/config/Cp_GITLAB_HOOK_SECRET.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) %today.year Kapralov Sergey | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
* | ||
*/ | ||
|
||
package com.github.skapral.puzzler.gitlab.config; | ||
|
||
import com.github.skapral.puzzler.core.config.CpEnvironment; | ||
|
||
/** | ||
* GITLAB_HOOK_SECRET environment variable's value | ||
* | ||
* @author Kapralov Sergey | ||
*/ | ||
public class Cp_GITLAB_HOOK_SECRET extends CpEnvironment { | ||
/** | ||
* Ctor. | ||
*/ | ||
public Cp_GITLAB_HOOK_SECRET() { | ||
super("GITLAB_HOOK_SECRET"); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...c/main/java/com/github/skapral/puzzler/gitlab/operation/OpValidatingGitlabEventToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) %today.year Kapralov Sergey | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
* | ||
*/ | ||
|
||
package com.github.skapral.puzzler.gitlab.operation; | ||
|
||
import com.github.skapral.puzzler.core.Operation; | ||
import com.github.skapral.puzzler.core.config.ConfigProperty; | ||
|
||
import java.util.function.Function; | ||
|
||
/** | ||
* Operation which is executed only if GitLab event token | ||
* match on GitLab event. See <a href="https://docs.gitlab.com/ee/user/project/integrations/webhooks.html#secret-token"></a> | ||
* for details. | ||
* | ||
* @author Kapralov Sergey | ||
*/ | ||
public class OpValidatingGitlabEventToken implements Operation { | ||
private final ConfigProperty expectedToken; | ||
private final String eventToken; | ||
private final Operation delegate; | ||
private final Function<String, RuntimeException> exceptionFn; | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param expectedToken Secret. If the property value is not provided, validation will be skept. | ||
* @param eventToken Event token (X-Gitlab-Token) | ||
* @param delegate Operation to delegate to, if signatures match. | ||
* @param exceptionFn Constructor of exception to throw if signatures mismatch. | ||
*/ | ||
public OpValidatingGitlabEventToken(ConfigProperty expectedToken, String eventToken, Operation delegate, Function<String, RuntimeException> exceptionFn) { | ||
this.expectedToken = expectedToken; | ||
this.eventToken = eventToken; | ||
this.delegate = delegate; | ||
this.exceptionFn = exceptionFn; | ||
} | ||
|
||
@Override | ||
public final void execute() { | ||
if(expectedToken.optionalValue().isEmpty()) { | ||
delegate.execute(); | ||
} else { | ||
if (expectedToken.optionalValue().get().equals(eventToken)) { | ||
delegate.execute(); | ||
} else { | ||
throw exceptionFn.apply("Token mismatch"); | ||
} | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
puzzler-gitlab/src/main/java/com/github/skapral/puzzler/gitlab/operation/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) %today.year Kapralov Sergey | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
* | ||
*/ | ||
|
||
package com.github.skapral.puzzler.gitlab.operation; |
64 changes: 64 additions & 0 deletions
64
...st/java/com/github/skapral/puzzler/gitlab/operation/OpValidatingGitlabEventTokenTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) %today.year Kapralov Sergey | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
* | ||
*/ | ||
|
||
package com.github.skapral.puzzler.gitlab.operation; | ||
|
||
|
||
import com.github.skapral.puzzler.core.config.CpStatic; | ||
import com.github.skapral.puzzler.core.operation.AssertOperationFails; | ||
import com.github.skapral.puzzler.core.operation.AssertOperationSuccessful; | ||
import com.github.skapral.puzzler.core.operation.OpDoNothing; | ||
import com.pragmaticobjects.oo.tests.TestCase; | ||
import com.pragmaticobjects.oo.tests.junit5.TestsSuite; | ||
|
||
class OpValidatingGitlabEventTokenTest extends TestsSuite { | ||
public OpValidatingGitlabEventTokenTest() { | ||
super( | ||
new TestCase( | ||
"Positive case", | ||
new AssertOperationSuccessful( | ||
new OpValidatingGitlabEventToken( | ||
new CpStatic("qwerty"), | ||
"qwerty", | ||
new OpDoNothing(), | ||
RuntimeException::new | ||
) | ||
) | ||
), | ||
new TestCase( | ||
"Negative case", | ||
new AssertOperationFails( | ||
new OpValidatingGitlabEventToken( | ||
new CpStatic("qwerty"), | ||
"asdfgh", | ||
new OpDoNothing(), | ||
RuntimeException::new | ||
) | ||
) | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters