3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.alm.client.github.security;
22 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
23 import java.io.IOException;
24 import java.security.spec.InvalidKeySpecException;
25 import java.time.Clock;
26 import java.time.Instant;
27 import java.time.ZoneId;
28 import java.util.Random;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.sonar.auth.github.GithubAppConfiguration;
33 import static org.apache.commons.lang3.RandomStringUtils.secure;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.core.api.Assertions.assertThatThrownBy;
37 @RunWith(DataProviderRunner.class)
38 public class GithubAppSecurityImplTest {
39 private Clock clock = Clock.fixed(Instant.ofEpochSecond(132_600_888L), ZoneId.systemDefault());
40 private GithubAppSecurityImpl underTest = new GithubAppSecurityImpl(clock);
43 public void createAppToken_fails_with_IAE_if_privateKey_content_is_garbage() {
44 String garbage = secure().nextAlphanumeric(555);
45 GithubAppConfiguration githubAppConfiguration = createAppConfigurationForPrivateKey(garbage);
47 assertThatThrownBy(() -> underTest.createAppToken(githubAppConfiguration.getId(), githubAppConfiguration.getPrivateKey()))
48 .isInstanceOf(IllegalArgumentException.class)
49 .hasRootCauseMessage("Failed to decode Github Application private key");
54 public void createAppToken_fails_with_IAE_if_privateKey_PKCS8_content_is_missing_end_comment() {
55 String incompletePrivateKey = "-----BEGIN RSA PRIVATE KEY-----\n"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3\n" +
71 "g+OmQxORXMmBkHEnYS1fl3ac3U5sLiHAQBmTKKcLuVQlIU4oDu/K6WEGL9DdPtaK\n" +
72 "gyOOWtSnfHTbT0bZ4IMm+gzdc4bCuEjvYyUhzG0CgYAEN011MAyTqFSvAwN9kjhb\n" +
73 "deYVmmL57GQuF6FP+/S7RgChpIQqimdS4vb7wFYlfaKtNq1V9jwoh51S0kt8qO7n\n" +
74 "ujEHJ2aBnwKJYJbBGV+hBvK/vbvG0TmotaWspmJJ+G6QigHx/Te+0Maw4PO+zTjo\n" +
75 "pdeP8b3JW70LkC+iKBp3swKBgFL/nm32m1tHEjFtehpVHFkSg05Z+jJDATiKlhh0\n" +
76 "YS2Vz+yuTDpE54CFW4M8wZKnXNbWJDBdd6KjIu42kKrA/zTJ5Ox92u1BJXFsk9fk\n" +
77 "xcX++qp5iBGepXZgHEiBMQLcdgY1m3jQl6XXOGSFog0+c4NIE/f1A8PrwI7gAdSt\n" +
78 "56SVAoGBAJp214Fo0oheMTTYKVtXuGiH/v3JNG1jKFgsmHqndf4wy7U6bbNctEzc\n" +
79 "ZXNIacuhWmko6YejMrWNhE57sX812MhXGZq6y0sYZGKtp7oDv8G3rWD6bpZywpcV\n" +
80 "kTtMJxm8J64u6bAkpWG3BocJP9qbXeAbILo1wuXgYqABBrpA9nnc";
81 GithubAppConfiguration githubAppConfiguration = createAppConfigurationForPrivateKey(incompletePrivateKey);
83 assertThatThrownBy(() -> underTest.createAppToken(githubAppConfiguration.getId(), githubAppConfiguration.getPrivateKey()))
84 .isInstanceOf(IllegalArgumentException.class)
85 .hasRootCauseInstanceOf(IOException.class)
86 .hasRootCauseMessage("-----END RSA PRIVATE KEY----- not found");
90 public void createAppToken_fails_with_IAE_if_privateKey_PKCS8_content_is_corrupted() {
91 String corruptedPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\n" +
92 "MIIEowIBAAKCAQEA6C29ZdvrwHOu7Eewv+xvUd4inCnACTzAHukHKTSY4R16+lRI\n" +
93 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L9DdPtaK\n" +
108 "gyOOWtSnfHTbT0bZ4IMm+gzdc4bCuEjvYyUhzG0CgYAEN011MAyTqFSvAwN9kjhb\n" +
109 "deYVmmL57GQuF6FP+/S7RgChpIQqimdS4vb7wFYlfaKtNq1V9jwoh51S0kt8qO7n\n" +
110 "ujEHJ2aBnwKJYJbBGV+hBvK/vbvG0TmotaWspmJJ+G6QigHx/Te+0Maw4PO+zTjo\n" +
111 "pdeP8b3JW70LkC+iKBp3swKBgFL/nm32m1tHEjFtehpVHFkSg05Z+jJDATiKlhh0\n" +
112 "YS2Vz+yuTDpE54CFW4M8wZKnXNbWJDBdd6KjIu42kKrA/zTJ5Ox92u1BJXFsk9fk\n" +
113 "xcX++qp5iBGepXZgHEiBMQLcdgY1m3jQl6XXOGSFog0+c4NIE/f1A8PrwI7gAdSt\n" +
114 "56SVAoGBAJp214Fo0oheMTTYKVtXuGiH/v3JNG1jKFgsmHqndf4wy7U6bbNctEzc\n" +
115 "ZXNIacuhWmko6YejMrWNhE57sX812MhXGZq6y0sYZGKtp7oDv8G3rWD6bpZywpcV\n" +
116 "kTtMJxm8J64u6bAkpWG3BocJP9qbXeAbILo1wuXgYqABBrpA9nnc\n" +
117 "-----END RSA PRIVATE KEY-----";
118 GithubAppConfiguration githubAppConfiguration = createAppConfigurationForPrivateKey(corruptedPrivateKey);
120 assertThatThrownBy(() -> underTest.createAppToken(githubAppConfiguration.getId(), githubAppConfiguration.getPrivateKey()))
121 .isInstanceOf(IllegalArgumentException.class)
122 .hasCauseInstanceOf(InvalidKeySpecException.class);
126 public void getApplicationJWTToken_throws_ISE_if_conf_is_not_complete() {
127 GithubAppConfiguration githubAppConfiguration = createAppConfiguration(false);
128 assertThatThrownBy(() -> underTest.createAppToken(githubAppConfiguration.getId(), githubAppConfiguration.getPrivateKey()))
129 .isInstanceOf(IllegalStateException.class);
133 public void getApplicationJWTToken_returns_token_if_app_config_and_private_key_are_valid() {
134 GithubAppConfiguration githubAppConfiguration = createAppConfiguration(true);
136 assertThat(underTest.createAppToken(githubAppConfiguration.getId(), githubAppConfiguration.getPrivateKey())).isNotNull();
139 private GithubAppConfiguration createAppConfiguration(boolean validConfiguration) {
140 if (validConfiguration) {
141 return createAppConfiguration();
143 return new GithubAppConfiguration(null, null, null);
147 private GithubAppConfiguration createAppConfiguration() {
148 return new GithubAppConfiguration(new Random().nextLong(), REAL_PRIVATE_KEY, secure().nextAlphanumeric(5));
151 private GithubAppConfiguration createAppConfigurationForPrivateKey(String privateKey) {
152 long applicationId = new Random().nextInt(654);
153 return new GithubAppConfiguration(applicationId, privateKey, secure().nextAlphabetic(8));
156 private static final String REAL_PRIVATE_KEY = "-----BEGIN RSA PRIVATE KEY-----\n" +
157 "MIIEowIBAAKCAQEA6C29ZdvrwHOu7Eewv+xvUd4inCnACTzAHukHKTSY4R16+lRI\n" +
158 "YC5qZ8Xo304J7lLhN4/d4Xnof3lDXZOHthVbJKik4fOuEGbTXTIcuFs3hdJtrJsb\n" +
159 "antv8SOl5iR4fYRAf2AILMdtZI4iMSicBLIIttR+wVXo6NJYMjpj1OuAU3uN8eET\n" +
160 "Gge09oJT3QOUBem7N8uaYi/p5uAfsf2/SVNsoMPV624X4kgNcyj/TMa6BosFJ8Y3\n" +
161 "oeg0Aguk2yuHhAnixDVGoz6N7Go0QjEipVNix2JOOJwpFH4k2iZfM6n+8sJTLilq\n" +
162 "yzT53JW/XI+M5AXVj4OjBJ/2yMPi3RFMNTdgRwIDAQABAoIBACcYBIsRI7oNAIgi\n" +
163 "bh1y1y+mwpce5Inpo8PQovcKNy+4gguCg4lGZ34/sb1f64YoiGmNnOOpXj+QkIpC\n" +
164 "HBjJscYTa2fsWwPB/Jb1qCZWnZu32eW1XEFqtWeaBAYjX/JqgV2xMs8vaTkEQbeb\n" +
165 "SeH0hEkcsJcnOwdw247hjAu+96WWlyt10ZGgQaWPfXsdtelbaoaturNAVAJHdl9e\n" +
166 "TIknCIbtLlbz/FtzjtCtdeiWr8gbKdVkshGtA8SKV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bILo1wuXgYqABBrpA9nnc\n" +
182 "-----END RSA PRIVATE KEY-----";