-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Included tests for GitHub attestations
- Included tests for GitHub attestations and some simple clean up. Signed-off-by: naveensrinivasan <[email protected]>
- Loading branch information
1 parent
5e567f0
commit f69933d
Showing
3 changed files
with
180 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// Copyright 2021 The Witness Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package github | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/testifysec/go-witness/attestation" | ||
) | ||
|
||
func createMockServer() *httptest.Server { | ||
type Response struct { | ||
Count int `json:"count"` | ||
Value string `json:"value"` | ||
} | ||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.URL.Path == "/valid" && r.Header.Get("Authorization") == "bearer validBearer" { | ||
resp, _ := json.Marshal(Response{Count: 1, Value: "validJWTToken"}) | ||
_, _ = w.Write(resp) | ||
} else { | ||
http.Error(w, "Unauthorized", http.StatusUnauthorized) | ||
} | ||
})) | ||
} | ||
|
||
func createTokenServer() *httptest.Server { | ||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.URL.Path == "/valid" && r.Header.Get("Authorization") == "bearer validBearer" { | ||
w.Write([]byte(`{"protected": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9", | ||
"payload": "eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ", | ||
"signature": "SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"}`)) | ||
} else { | ||
http.Error(w, "Unauthorized", http.StatusUnauthorized) | ||
} | ||
})) | ||
} | ||
|
||
func TestFetchToken(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
tokenURL string | ||
bearer string | ||
audience string | ||
wantToken string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid token", | ||
tokenURL: "/valid", | ||
bearer: "validBearer", | ||
audience: "validAudience", | ||
wantToken: "validJWTToken", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid token url", | ||
tokenURL: "/invalid", | ||
bearer: "validBearer", | ||
audience: "validAudience", | ||
wantToken: "", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "invalid bearer", | ||
tokenURL: "/valid", | ||
bearer: "invalidBearer", | ||
audience: "validAudience", | ||
wantToken: "", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "invalid url", | ||
tokenURL: "invalidURL", | ||
bearer: "validBearer", | ||
audience: "validAudience", | ||
wantToken: "", | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
server := createMockServer() | ||
defer server.Close() | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
gotToken, err := fetchToken(server.URL+testCase.tokenURL, testCase.bearer, testCase.audience) | ||
if testCase.wantErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, testCase.wantToken, gotToken) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAttestorAttest(t *testing.T) { | ||
tokenServer := createTokenServer() | ||
defer tokenServer.Close() | ||
t.Setenv("GITHUB_ACTIONS", "true") | ||
t.Setenv("ACTIONS_ID_TOKEN_REQUEST_URL", tokenServer.URL+"/valid") | ||
t.Setenv("ACTIONS_ID_TOKEN_REQUEST_TOKEN", "validBearer") | ||
|
||
attestor := &Attestor{ | ||
aud: tokenAudience, | ||
jwksURL: tokenServer.URL, | ||
tokenURL: os.Getenv("ACTIONS_ID_TOKEN_REQUEST_URL"), | ||
} | ||
|
||
ctx := &attestation.AttestationContext{} | ||
|
||
err := attestor.Attest(ctx) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestSubjects(t *testing.T) { | ||
tokenServer := createTokenServer() | ||
defer tokenServer.Close() | ||
attestor := &Attestor{ | ||
aud: "projecturl", | ||
jwksURL: tokenServer.URL, | ||
tokenURL: os.Getenv("ACTIONS_ID_TOKEN_REQUEST_URL"), | ||
} | ||
|
||
subjects := attestor.Subjects() | ||
assert.NotNil(t, subjects) | ||
assert.Equal(t, 2, len(subjects)) | ||
|
||
expectedSubjects := []string{"pipelineurl:" + attestor.PipelineUrl, "projecturl:" + attestor.ProjectUrl} | ||
for _, expectedSubject := range expectedSubjects { | ||
_, ok := subjects[expectedSubject] | ||
assert.True(t, ok, "Expected subject not found: %s", expectedSubject) | ||
} | ||
m := attestor.BackRefs() | ||
assert.NotNil(t, m) | ||
assert.Equal(t, 1, len(m)) | ||
} |
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