pax_global_header00006660000000000000000000000064151544770570014531gustar00rootroot0000000000000052 comment=5a8790b283fafd98b89f2ab42b96c258334a88fe omnibor-go-0.0~git20230521.a77de61/000077500000000000000000000000001515447705700162365ustar00rootroot00000000000000omnibor-go-0.0~git20230521.a77de61/.github/000077500000000000000000000000001515447705700175765ustar00rootroot00000000000000omnibor-go-0.0~git20230521.a77de61/.github/workflows/000077500000000000000000000000001515447705700216335ustar00rootroot00000000000000omnibor-go-0.0~git20230521.a77de61/.github/workflows/go.yml000066400000000000000000000005451515447705700227670ustar00rootroot00000000000000name: Go on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Go uses: actions/setup-go@v2 with: go-version: 1.18 - name: Build run: go build -v ./... - name: Test run: go test -v ./... omnibor-go-0.0~git20230521.a77de61/.gitignore000066400000000000000000000010271515447705700202260ustar00rootroot00000000000000# If you prefer the allow list template instead of the deny list, see community template: # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore # # Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, built with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out # Dependency directories (remove the comment below to include it) # vendor/ # Go workspace file go.work .idea/ .gitbom/ # old directory, no longer in use .bom/ omnibor-go-0.0~git20230521.a77de61/LICENSE000066400000000000000000000357451515447705700172610ustar00rootroot00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright {yyyy} {name of copyright owner} 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. =============================================================================== The following components included on this website are distributed under MIT license : - Jekyll Bootstrap 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. ------------------ - jQuery and Sizzle.js Copyright jQuery Foundation and other contributors, https://jquery.org/ This software consists of voluntary contributions made by many individuals. For exact contribution history, see the revision history available at https://github.com/jquery/jquery The following license applies to all parts of this software except as documented below: ==== 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. ==== All files located in the node_modules and external directories are externally maintained libraries used by this software which have their own licenses; we recommend you read them, as their terms may differ from the terms above. ------------------ - Normalize.css Copyright (c) Nicolas Gallagher and Jonathan Neal 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. omnibor-go-0.0~git20230521.a77de61/cmd/000077500000000000000000000000001515447705700170015ustar00rootroot00000000000000omnibor-go-0.0~git20230521.a77de61/cmd/omnibor/000077500000000000000000000000001515447705700204465ustar00rootroot00000000000000omnibor-go-0.0~git20230521.a77de61/cmd/omnibor/main.go000066400000000000000000000003131515447705700217160ustar00rootroot00000000000000package main import ( omnibor "github.com/omnibor/omnibor-go/pkg/cmd" "log" ) func main() { log.SetFlags(log.Flags() | log.Lshortfile) if err := omnibor.Run(); err != nil { log.Fatalln(err) } } omnibor-go-0.0~git20230521.a77de61/go.mod000066400000000000000000000014611515447705700173460ustar00rootroot00000000000000module github.com/omnibor/omnibor-go go 1.18 require ( github.com/edwarnicke/gitoid v0.0.0-20220710194850-1be5bfda1f9d github.com/facebookgo/symwalk v0.0.0-20150726040526-42004b9f3222 github.com/stretchr/testify v1.7.1 ) require ( github.com/davecgh/go-spew v1.1.0 // indirect github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c // indirect github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 // indirect github.com/facebookgo/testname v0.0.0-20150612200628-5443337c3a12 // indirect github.com/kr/pretty v0.1.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) omnibor-go-0.0~git20230521.a77de61/go.sum000066400000000000000000000057171515447705700174030ustar00rootroot00000000000000github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/edwarnicke/gitoid v0.0.0-20220710194850-1be5bfda1f9d h1:4l+Uq5zFWSagXgGFaKRRVWJrnlzeathyagWgYUltCgY= github.com/edwarnicke/gitoid v0.0.0-20220710194850-1be5bfda1f9d/go.mod h1:WxWwA3EYuCQjlR5EBUX3uaTS8bh9BOa7BcqVREHQ0uQ= github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c h1:8ISkoahWXwZR41ois5lSJBSVw4D0OV19Ht/JSTzvSv0= github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c/go.mod h1:Yg+htXGokKKdzcwhuNDwVvN+uBxDGXJ7G/VN1d8fa64= github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 h1:JWuenKqqX8nojtoVVWjGfOF9635RETekkoH6Cc9SX0A= github.com/facebookgo/stack v0.0.0-20160209184415-751773369052/go.mod h1:UbMTZqLaRiH3MsBH8va0n7s1pQYcu3uTb8G4tygF4Zg= github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 h1:7HZCaLC5+BZpmbhCOZJ293Lz68O7PYrF2EzeiFMwCLk= github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0= github.com/facebookgo/symwalk v0.0.0-20150726040526-42004b9f3222 h1:ivxAxcE9py2xLAqpcEwN7sN711aLfEWgh3cY0aha7uY= github.com/facebookgo/symwalk v0.0.0-20150726040526-42004b9f3222/go.mod h1:PgrCjL2+FgkITqxQI+erRTONtAv4JkpOzun5ozKW/Jg= github.com/facebookgo/testname v0.0.0-20150612200628-5443337c3a12 h1:pKeuUgeuL6jk/FpxSr0ZVL1XEiOmrcWBvB2rKXu0mMI= github.com/facebookgo/testname v0.0.0-20150612200628-5443337c3a12/go.mod h1:IYed2VYeQcs7JTN6KiVXjaz6Rv/Qz092Wjc6o5bCJ9I= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= omnibor-go-0.0~git20230521.a77de61/omnibor.go000066400000000000000000000177341515447705700202460ustar00rootroot00000000000000/* Package omnibor implements OmniBOR. Read the spec at https://github.com/omnibor/spec/blob/main/SPEC.md It is an application of the git DAG, a widely used merkle tree with a flat-file storage format, to the challenge of creating build artifact trees in today’s language-heterogeneous open source environments. by generating artifact trees at build time, embedding the hash of the tree in produced artifacts, and referencing that hash in the next build step, OmniBOR will enable the zero-end-user-effort creation of verifiable build trees. Furthermore, it will enable launch-time comparison of vulnerability data against a complete artifact tree for both open source and proprietary projects (if vuln data is traceable back to source file). Objective It is desirable to enable efficient launch-time comparison of the verifiable and complete build tree of any executable component [1] against a then-current list of undesirable source files [2] which are known to be undesirable, where such a build tree contains unique referents for all sources from which the given executable object was composed. [1]: binary, dynamically-linked library, container image, etc. [2]: because vulnerabilities may be discovered between the time an executable is created and the time when it is run, these processes must be decoupled */ package omnibor import ( "bytes" "encoding/hex" "fmt" "io" "sort" "strings" "sync" "github.com/edwarnicke/gitoid" ) // ArtifactTree provides a common interface that assists with the creation and management of an OmniBOR document. type ArtifactTree interface { Identifier // AddReference adds a SHA1+SHA256 based git reference to the current OmniBOR document. // obj []byte is the byte array to be tagged in the GitRef. // bom Identifier is the omnibor identifier of the artifact tree used to create the object. // The resulting reference is based on the GitRef format. // It returns an error if the SHA1 or SHA256 implementations fails. AddReference(obj []byte, bom Identifier) error // AddReferenceFromReader adds a SHA1+SHA256 based git reference to the current OmniBOR document. // The resulting reference is based on the GitRef format. // The io.Reader will be continuously be read until the reader returns a non-null error. // If the io.Reader returns io.EOF, the read is considered to be complete. // Any other return value from Reader is an error. // The object length must be included. // If the amount of bytes read does not match the stated object length, an error is returned. AddReferenceFromReader(reader io.Reader, bom Identifier, objLength int64) error // AddExistingReference adds an existing pre-computed reference // The string must be a valid gitoid identifier. AddExistingReference(s string) error // References Returns a lsit of references in the order it will be printed. References() []Reference // String Returns the string representation of the OmniBOR. String() string } type Reference interface { // Identity returns the GitRef identity of the object as a hex string. Identity() string // Bom returns an Identifier representing the dependency tree of the object represented by the Identity Bom() Identifier // String returns a ArtifactTree entry represented by this Reference. String() string } func referenceSorter(r1, r2 Reference) bool { return r1.Identity() < r2.Identity() } type by func(p1, p2 Reference) bool func (b by) sort(refs []Reference) { sorter := &referenceSort{ refs: refs, by: b, } sort.Sort(sorter) } type reference struct { hashType string identity string bom Identifier } type referenceSort struct { refs []Reference by by } func (grs *referenceSort) Len() int { return len(grs.refs) } func (grs *referenceSort) Swap(i, j int) { grs.refs[i], grs.refs[j] = grs.refs[j], grs.refs[i] } func (grs *referenceSort) Less(i, j int) bool { return grs.by(grs.refs[i], grs.refs[j]) } func (ref reference) Identity() string { return ref.identity } func (ref reference) Bom() Identifier { return ref.Bom() } func (ref reference) String() string { res := fmt.Sprintf("blob %s", ref.identity) if ref.bom != nil { res = fmt.Sprintf("%s bom %s", res, ref.bom.Identity()) } res = res + "\n" return res } type Identifier interface { Identity() string } type omniBor struct { lock sync.Mutex gitRefs []Reference gitoidOptions []gitoid.Option hashType string } // NewSha1OmniBOR creates a new ArtifactTree object. // Thread Safety: none, apply your own controls. // // Adding duplicate objects with the same Reference identity results in only one Reference entry. // References are sorted in ascending order based on their UTF-8 values. // // Implementation details: // Adding a Reference is O(n) to discover duplicates. // Generating a ArtifactTree is O(n*log(n)) as it sorts the existing refs. func NewSha1OmniBOR() ArtifactTree { return &omniBor{ hashType: "sha1", } } func NewSha256OmniBOR() ArtifactTree { options := []gitoid.Option{gitoid.WithSha256()} return &omniBor{ gitoidOptions: options, hashType: "sha256", } } func (srv *omniBor) AddReference(obj []byte, bom Identifier) error { reader := bytes.NewBuffer(obj) return srv.addGitRef(reader, bom, int64(len(obj))) } func (srv *omniBor) AddReferenceFromReader(reader io.Reader, bom Identifier, objLength int64) error { return srv.addGitRef(reader, bom, objLength) } func (srv *omniBor) AddExistingReference(input string) error { // if srv is using sha1, check that the input is a valid hex sha1 and length // if srv is in sha256 mode, set hashLength to the length of a sha256 hash hashLength := 40 if srv.hashType == "sha256" { hashLength = 64 } if len(input) != hashLength { return fmt.Errorf("invalid hash length: %d", len(input)) } if _, err := hex.DecodeString(input); err != nil { return err } ref := reference{ identity: input, } // check if the input is already in the gitRefs list for _, existingRef := range srv.gitRefs { if existingRef.Identity() == input { return nil } } srv.lock.Lock() srv.gitRefs = append(srv.gitRefs, ref) srv.lock.Unlock() return nil } func (srv *omniBor) addGitRef(reader io.Reader, bom Identifier, length int64) error { // add an initial option specifying the length options := []gitoid.Option{ gitoid.WithContentLength(length), } // populate any options we need for _, option := range srv.gitoidOptions { options = append(options, option) } identity, err := gitoid.New(reader, options...) if err != nil { return err } ref := reference{ identity: identity.String(), bom: bom, } srv.lock.Lock() srv.gitRefs = append(srv.gitRefs, ref) srv.lock.Unlock() return nil } func (srv *omniBor) References() []Reference { srv.lock.Lock() by(referenceSorter).sort(srv.gitRefs) result := make([]Reference, 0, len(srv.gitRefs)) for _, ref := range srv.gitRefs { result = append(result, ref) } srv.lock.Unlock() return srv.gitRefs } func (srv *omniBor) String() string { srv.lock.Lock() by(referenceSorter).sort(srv.gitRefs) refs := make([]string, 0) for _, ref := range srv.gitRefs { refs = append(refs, ref.String()) } srv.lock.Unlock() return strings.Join(refs, "") } func (srv *omniBor) gitRef() string { generated := srv.String() // add an initial option specifying the length options := []gitoid.Option{ gitoid.WithContentLength(int64(len(generated))), } // populate any options we need for _, option := range srv.gitoidOptions { options = append(options, option) } res, err := gitoid.New(bytes.NewBuffer([]byte(generated)), options...) if err != nil { // we should only see this if the runtime was fundamentally broken panic(err) } return res.String() } func (srv *omniBor) Identity() string { return srv.gitRef() } type identifier struct { identity string } func (gb identifier) Identity() string { return gb.identity } func NewIdentifier(identity string) (Identifier, error) { // TODO check if omnibor matches the format _, err := hex.DecodeString(identity) if err != nil { return nil, err } return &identifier{ identity: identity, }, nil } omnibor-go-0.0~git20230521.a77de61/omnibor_test.go000066400000000000000000000203321515447705700212710ustar00rootroot00000000000000package omnibor import ( "bytes" "encoding/binary" "fmt" "github.com/edwarnicke/gitoid" "testing" "github.com/stretchr/testify/assert" ) // TODO create sha256 version func TestFlatWorkflowSha1(t *testing.T) { string1 := "hello" string2 := "world" gb := NewSha1OmniBOR() err := gb.AddReferenceFromReader(bytes.NewBufferString(string1), nil, int64(len(string1))) assert.NoError(t, err) err = gb.AddReferenceFromReader(bytes.NewBufferString(string2), nil, int64(len(string2))) assert.NoError(t, err) expected := "blob 04fea06420ca60892f73becee3614f6d023a4b7f\n" + "blob b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0\n" assert.Equal(t, expected, gb.String()) ref := gb.Identity() assert.NoError(t, err) assert.Equal(t, "dc0be356e8c2ba26e66448d97db76ad050206574", ref) } func TestFlatWorkflowSha256(t *testing.T) { string1 := "hello" string2 := "world" gb := NewSha256OmniBOR() err := gb.AddReferenceFromReader(bytes.NewBufferString(string1), nil, int64(len(string1))) assert.NoError(t, err) err = gb.AddReferenceFromReader(bytes.NewBufferString(string2), nil, int64(len(string2))) assert.NoError(t, err) expected := "blob 8aec4e4876f854f688d0ebfc8f37598f38e5fd6903cccc850ca36591175aeb60\n" + "blob 8df3dab4ddfa6eb2a34065cda27d95af2709d4d2658e1b5fbd145822acf42b28\n" assert.Equal(t, expected, gb.String()) ref := gb.Identity() assert.NoError(t, err) assert.Equal(t, "e32e7e7761709be17ef573556a82960d489ddf0092424f7db1c91d8363dde822", ref) } // TODO add sha256 func TestNestedWorkflowSha1(t *testing.T) { string1 := "hello" string2 := "world" gb := NewSha1OmniBOR() err := gb.AddReferenceFromReader(bytes.NewBufferString(string1), nil, int64(len(string1))) assert.NoError(t, err) err = gb.AddReferenceFromReader(bytes.NewBufferString(string2), nil, int64(len(string2))) assert.NoError(t, err) expected := "blob 04fea06420ca60892f73becee3614f6d023a4b7f\nblob b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0\n" assert.Equal(t, expected, gb.String()) ref := gb.Identity() expected = "dc0be356e8c2ba26e66448d97db76ad050206574" assert.Equal(t, expected, ref) string3 := "hello2" string4 := "independent" string5 := "opaque" gb2 := NewSha1OmniBOR() err = gb2.AddReference([]byte(string3), gb) assert.NoError(t, err) err = gb2.AddReference([]byte(string4), nil) assert.NoError(t, err) expected = "blob 23294b0610492cf55c1c4835216f20d376a287dd bom dc0be356e8c2ba26e66448d97db76ad050206574\nblob be78cc5602c5457f144a67e574b8f98b9dc2a1a0\n" assert.Equal(t, expected, gb2.String()) identifier, err := NewIdentifier("a87d2b20b13568a5530ec6a59dacfdda8ee3cd1e3d63c9d13da26d27e3447812") assert.NoError(t, err) err = gb2.AddReference([]byte(string5), identifier) assert.NoError(t, err) expected = "blob 23294b0610492cf55c1c4835216f20d376a287dd bom dc0be356e8c2ba26e66448d97db76ad050206574\nblob 32898208a218272b0fa7549f60951d4eed2ed830 bom a87d2b20b13568a5530ec6a59dacfdda8ee3cd1e3d63c9d13da26d27e3447812\nblob be78cc5602c5457f144a67e574b8f98b9dc2a1a0\n" assert.Equal(t, expected, gb2.String()) } func TestMixedNestedWorkflow(t *testing.T) { string1 := "hello" string2 := "world" gb := NewSha256OmniBOR() err := gb.AddReferenceFromReader(bytes.NewBufferString(string1), nil, int64(len(string1))) assert.NoError(t, err) err = gb.AddReferenceFromReader(bytes.NewBufferString(string2), nil, int64(len(string2))) assert.NoError(t, err) expected := "blob 8aec4e4876f854f688d0ebfc8f37598f38e5fd6903cccc850ca36591175aeb60\nblob 8df3dab4ddfa6eb2a34065cda27d95af2709d4d2658e1b5fbd145822acf42b28\n" assert.Equal(t, expected, gb.String()) ref := gb.Identity() expected = "e32e7e7761709be17ef573556a82960d489ddf0092424f7db1c91d8363dde822" assert.Equal(t, expected, ref) string3 := "hello2" string4 := "independent" string5 := "opaque" gb2 := NewSha1OmniBOR() err = gb2.AddReference([]byte(string3), gb) assert.NoError(t, err) err = gb2.AddReference([]byte(string4), nil) assert.NoError(t, err) expected = "blob 23294b0610492cf55c1c4835216f20d376a287dd bom e32e7e7761709be17ef573556a82960d489ddf0092424f7db1c91d8363dde822\nblob be78cc5602c5457f144a67e574b8f98b9dc2a1a0\n" assert.Equal(t, expected, gb2.String()) identifier, err := NewIdentifier("a87d2b20b13568a5530ec6a59dacfdda8ee3cd1e3d63c9d13da26d27e3447812") assert.NoError(t, err) err = gb2.AddReference([]byte(string5), identifier) assert.NoError(t, err) expected = "blob 23294b0610492cf55c1c4835216f20d376a287dd bom e32e7e7761709be17ef573556a82960d489ddf0092424f7db1c91d8363dde822\nblob 32898208a218272b0fa7549f60951d4eed2ed830 bom a87d2b20b13568a5530ec6a59dacfdda8ee3cd1e3d63c9d13da26d27e3447812\nblob be78cc5602c5457f144a67e574b8f98b9dc2a1a0\n" assert.Equal(t, expected, gb2.String()) } func TestValidIdentifier(t *testing.T) { _, err := NewIdentifier("23294b0610492cf55c1c4835216f20d376a287dd") assert.NoError(t, err) } func TestInvalidIdentifier_TooFewCharacters(t *testing.T) { _, err := NewIdentifier("23294b0610492cf55c1c4835216f20d376a287d") assert.Error(t, err) } func TestInvalidIdentifier_NonHexCharacter(t *testing.T) { _, err := NewIdentifier("23294b0610492cf55c1c4835216f20d376a287dg") assert.Error(t, err) } func TestInvalidIdentifier_ExtraTrailingSpace(t *testing.T) { _, err := NewIdentifier("23294b0610492cf55c1c4835216f20d376a287dd ") assert.Error(t, err) } func TestInvalidIdentifier_ExtraSpaces(t *testing.T) { _, err := NewIdentifier(" 23294b0610492cf55c1c4835216f20d376a287dd ") assert.Error(t, err) } func TestInvalidIdentifier_VeryInvalid(t *testing.T) { _, err := NewIdentifier(" 23294b0610492cf 55c1c4835216f20d376a287dd ") assert.Error(t, err) } func TestAddingExistingReferenceSha1(t *testing.T) { string1 := "hello" string2 := "world" gid1, _ := gitoid.New(bytes.NewBufferString(string1)) gid2, _ := gitoid.New(bytes.NewBufferString(string2)) gb := NewSha1OmniBOR() err := gb.AddExistingReference(gid1.String()) assert.NoError(t, err) err = gb.AddExistingReference(gid2.String()) assert.NoError(t, err) expected := "blob 04fea06420ca60892f73becee3614f6d023a4b7f\nblob b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0\n" assert.Equal(t, expected, gb.String()) } func TestAddingExistingReferenceSha256(t *testing.T) { string1 := "hello" string2 := "world" gid1, _ := gitoid.New(bytes.NewBufferString(string1), gitoid.WithSha256()) gid2, _ := gitoid.New(bytes.NewBufferString(string2), gitoid.WithSha256()) gb := NewSha256OmniBOR() err := gb.AddExistingReference(gid1.String()) assert.NoError(t, err) err = gb.AddExistingReference(gid2.String()) assert.NoError(t, err) expected := "blob 8aec4e4876f854f688d0ebfc8f37598f38e5fd6903cccc850ca36591175aeb60\nblob 8df3dab4ddfa6eb2a34065cda27d95af2709d4d2658e1b5fbd145822acf42b28\n" assert.Equal(t, expected, gb.String()) } func TestAddExistingMalformedSha1(t *testing.T) { string1 := "hello" gid1, _ := gitoid.New(bytes.NewBufferString(string1)) gb := NewSha1OmniBOR() err := gb.AddExistingReference(gid1.String()[1:]) assert.Error(t, err) malformedHash := gid1.String() malformedHash = "g" + malformedHash[1:] err = gb.AddExistingReference(malformedHash) assert.Error(t, err) gid256, _ := gitoid.New(bytes.NewBufferString(string1), gitoid.WithSha256()) err = gb.AddExistingReference(gid256.String()) assert.Error(t, err) } func TestAddExistingMalformedSha256(t *testing.T) { string1 := "hello" gid1, _ := gitoid.New(bytes.NewBufferString(string1), gitoid.WithSha256()) gb := NewSha256OmniBOR() err := gb.AddExistingReference(gid1.String()[1:]) assert.Error(t, err) malformedHash := gid1.String() malformedHash = "g" + malformedHash[1:] err = gb.AddExistingReference(malformedHash) assert.Error(t, err) gidsha1, _ := gitoid.New(bytes.NewBufferString(string1)) err = gb.AddExistingReference(gidsha1.String()) assert.Error(t, err) } func BenchmarkNewOmniBOR(b *testing.B) { dataset := generateDataset(b.N) gb := NewSha1OmniBOR() b.ResetTimer() for i := 0; i < b.N; i++ { //fmt.Println(dataset[i]) _ = gb.AddReference(dataset[i], nil) } b.StopTimer() fmt.Println(len(gb.References()), len(dataset), b.N) } func generateDataset(n int) [][]byte { dataset := make([][]byte, 0) for i := 0; i < n; i++ { buf := make([]byte, 64) binary.LittleEndian.PutUint32(buf, uint32(i)) //fmt.Println(buf) dataset = append(dataset, buf) } for i := 0; i < len(dataset); i++ { //fmt.Println(dataset[i]) } return dataset } omnibor-go-0.0~git20230521.a77de61/pkg/000077500000000000000000000000001515447705700170175ustar00rootroot00000000000000omnibor-go-0.0~git20230521.a77de61/pkg/cmd/000077500000000000000000000000001515447705700175625ustar00rootroot00000000000000omnibor-go-0.0~git20230521.a77de61/pkg/cmd/omnibor_cmd.go000066400000000000000000000066651515447705700224160ustar00rootroot00000000000000package cmd import ( "fmt" "github.com/facebookgo/symwalk" omnibor "github.com/omnibor/omnibor-go" "io/ioutil" "log" "os" "path" "path/filepath" "runtime" "sync" ) func Run() error { if len(os.Args) < 2 { return helpCall() } if os.Args[1] == "artifact-tree" { return artifactTreeCall(os.Args[2:]...) } if os.Args[1] == "bom" { return artifactTreeCall(os.Args[2:]...) } return helpCall() } func helpCall() error { _, err := printHelp() return err } func artifactTreeCall(args ...string) error { wg := startAgents() if len(args) == 0 { _, err := printHelp() return err } gb := omnibor.NewSha1OmniBOR() for i := 0; i < len(args); i++ { if err := addPathToOmniBOR(gb, args[i], agentChan); err != nil { log.Println(args[i], err) return err } } close(agentChan) wg.Wait() // generate target omnibor with artifact tree if err := writeObject(".bom", gb); err != nil { log.Println(err) return err } fmt.Println(gb.Identity()) return nil } var agentChan = make(chan fileEvent) func startAgents() *sync.WaitGroup { agentCount := 0 wg := &sync.WaitGroup{} if runtime.GOMAXPROCS(0) < runtime.NumCPU() { agentCount = runtime.GOMAXPROCS(0) } else { agentCount = runtime.NumCPU() } for i := 0; i < agentCount; i++ { wg.Add(1) go agent(agentChan, wg) } return wg } func writeObject(prefix string, gb omnibor.ArtifactTree) error { objs := gb.Identity() objectDir := path.Join(prefix, "object", objs[0:2]) objectPath := path.Join(objectDir, objs[2:]) if err := os.MkdirAll(objectDir, 0755); err != nil { log.Println(err) return err } if err := ioutil.WriteFile(objectPath, []byte(gb.String()), 0644); err != nil { return err } return nil } func addPathToOmniBOR(gb omnibor.ArtifactTree, fileName string, agentChan chan<- fileEvent) error { err := symwalk.Walk(fileName, func(path string, info os.FileInfo, err error) error { if err != nil { return err } path, err = filepath.EvalSymlinks(path) if err != nil { log.Println("ERROR", err) return err } info, err = os.Stat(path) if err != nil { log.Println("ERROR", err) return err } if !info.IsDir() { e := fileEvent{ path: path, info: info, gb: gb, } agentChan <- e return nil } return nil }) return err } type fileEvent struct { path string info os.FileInfo gb omnibor.ArtifactTree } func agent(e <-chan fileEvent, wg *sync.WaitGroup) { defer wg.Done() for ev := range e { err2 := addFileToOmniBOR(ev.path, ev.info, ev.gb, nil) if err2 != nil { log.Println("ERROR", ev.path) } } } func addFileToOmniBOR(path string, info os.FileInfo, gb omnibor.ArtifactTree, identifier omnibor.Identifier) error { f, err := os.Open(path) if err != nil { return err } defer func(f *os.File) { err := f.Close() if err != nil { log.Printf("error closing %s: %s", path, err) } }(f) if err := gb.AddReferenceFromReader(f, identifier, info.Size()); err != nil { return err } return nil } func printHelp() (int, error) { return fmt.Println(` omnibor (v0.0.1) - Generate OmniBOR ADG from files **USAGE** omnibor artifact-tree [files] omnibor bom [artifact-file] [artifact-tree-files [artifact-tree files...]] omnibor will create a .bom/ directory in the current working directory and store generated OmniBOR ADGs in .bom/ **LEGAL** omnibor (v0.0.2) Copyright 2023 omnibor-go contributors SPDX-License-Identifier: Apache-2.0`) }