-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added functionality for scanning subjects
Signed-off-by: linus-sun <[email protected]>
- Loading branch information
Showing
14 changed files
with
250 additions
and
75 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
File renamed without changes.
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,53 @@ | ||
// Copyright 2024 The Sigstore Authors. | ||
// | ||
// 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 ct | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"regexp" | ||
|
||
ct "github.com/google/certificate-transparency-go" | ||
ctclient "github.com/google/certificate-transparency-go/client" | ||
"github.com/sigstore/rekor-monitor/pkg/identity" | ||
) | ||
|
||
func GetCTLogEntries(logClient *ctclient.LogClient, startIndex int, endIndex int) ([]ct.LogEntry, error) { | ||
entries, err := logClient.GetEntries(context.Background(), int64(startIndex), int64(endIndex)) | ||
if err != nil { | ||
return nil, fmt.Errorf("error retrieving certificate transparency log entries: %v", err) | ||
} | ||
return entries, nil | ||
} | ||
|
||
func ScanEntrySubject(logEntry ct.LogEntry, monitoredSubjects []string) ([]*identity.LogEntry, error) { | ||
subject := logEntry.X509Cert.Subject.String() | ||
foundEntries := []*identity.LogEntry{} | ||
for _, monitoredSub := range monitoredSubjects { | ||
regex, err := regexp.Compile(monitoredSub) | ||
if err != nil { | ||
return nil, fmt.Errorf("error compiling regex: %v", err) | ||
} | ||
matches := regex.FindAllString(subject, -1) | ||
for _, match := range matches { | ||
foundEntries = append(foundEntries, &identity.LogEntry{ | ||
Index: logEntry.Index, | ||
Subject: match, | ||
}) | ||
} | ||
} | ||
|
||
return foundEntries, nil | ||
} |
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,86 @@ | ||
// Copyright 2024 The Sigstore Authors. | ||
// | ||
// 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 ct | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
ct "github.com/google/certificate-transparency-go" | ||
"github.com/google/certificate-transparency-go/x509" | ||
"github.com/google/certificate-transparency-go/x509/pkix" | ||
"github.com/sigstore/rekor-monitor/pkg/identity" | ||
) | ||
|
||
const ( | ||
subjectName = "test-subject" | ||
organizationName = "test-org" | ||
) | ||
|
||
func TestScanEntrySubject(t *testing.T) { | ||
testCases := map[string]struct { | ||
inputEntry ct.LogEntry | ||
inputSubjects []string | ||
expected []*identity.LogEntry | ||
}{ | ||
"no matching subject": { | ||
inputEntry: ct.LogEntry{ | ||
Index: 1, | ||
X509Cert: &x509.Certificate{ | ||
Subject: pkix.Name{ | ||
CommonName: subjectName, | ||
}, | ||
}, | ||
}, | ||
inputSubjects: []string{}, | ||
expected: []*identity.LogEntry{}, | ||
}, | ||
"matching subject": { | ||
inputEntry: ct.LogEntry{ | ||
Index: 1, | ||
X509Cert: &x509.Certificate{ | ||
Subject: pkix.Name{ | ||
CommonName: subjectName, | ||
Organization: []string{organizationName}, | ||
}, | ||
}, | ||
}, | ||
inputSubjects: []string{subjectName, organizationName}, | ||
expected: []*identity.LogEntry{ | ||
{Index: 1, | ||
Subject: subjectName}, | ||
{Index: 1, | ||
Subject: organizationName}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
logEntries, err := ScanEntrySubject(tc.inputEntry, tc.inputSubjects) | ||
if err != nil { | ||
t.Errorf("received error scanning entry for subjects: %v", err) | ||
} | ||
expected := tc.expected | ||
if logEntries == nil { | ||
if expected != nil { | ||
t.Errorf("received nil, expected log entry") | ||
} | ||
} else { | ||
if !reflect.DeepEqual(logEntries, expected) { | ||
t.Errorf("expected %v, received %v", expected, logEntries) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.