diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 5432355b..d72031d9 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -40,16 +40,16 @@ jobs: steps: - name: Harden Runner - uses: step-security/harden-runner@a4aa98b93cab29d9b1101a6143fb8bce00e2eac4 # v2.7.1 + uses: step-security/harden-runner@17d0e2bd7d51742c71671bd19fa12bdc9d40a3d6 # v2.8.1 with: egress-policy: audit - name: Checkout repository - uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5 + uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@b7cec7526559c32f1616476ff32d17ba4c59b2d6 # v3.25.5 + uses: github/codeql-action/init@2e230e8fe0ad3a14a340ad0815ddb96d599d2aff # v3.25.8 with: languages: ${{ matrix.language }} @@ -57,7 +57,7 @@ jobs: make build - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@b7cec7526559c32f1616476ff32d17ba4c59b2d6 # v3.25.5 + uses: github/codeql-action/analyze@2e230e8fe0ad3a14a340ad0815ddb96d599d2aff # v3.25.8 - name: Generate Security Report uses: rsdmike/github-security-report-action@a149b24539044c92786ec39af8ba38c93496495d # v3.0.4 continue-on-error: true diff --git a/.github/workflows/dependency-review.yml b/.github/workflows/dependency-review.yml new file mode 100644 index 00000000..b9113179 --- /dev/null +++ b/.github/workflows/dependency-review.yml @@ -0,0 +1,27 @@ +# Dependency Review Action +# +# This Action will scan dependency manifest files that change as part of a Pull Request, +# surfacing known-vulnerable versions of the packages declared or updated in the PR. +# Once installed, if the workflow run is marked as required, +# PRs introducing known-vulnerable packages will be blocked from merging. +# +# Source repository: https://github.com/actions/dependency-review-action +name: 'Dependency Review' +on: [pull_request] + +permissions: + contents: read + +jobs: + dependency-review: + runs-on: ubuntu-latest + steps: + - name: Harden Runner + uses: step-security/harden-runner@17d0e2bd7d51742c71671bd19fa12bdc9d40a3d6 # v2.8.1 + with: + egress-policy: audit + + - name: 'Checkout Repository' + uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 + - name: 'Dependency Review' + uses: actions/dependency-review-action@72eb03d02c7872a771aacd928f3123ac62ad6d3a # v4.3.3 diff --git a/.github/workflows/docker-ci.yml b/.github/workflows/docker-ci.yml index d6b6b04a..48055030 100644 --- a/.github/workflows/docker-ci.yml +++ b/.github/workflows/docker-ci.yml @@ -21,16 +21,16 @@ jobs: steps: - name: Harden Runner - uses: step-security/harden-runner@a4aa98b93cab29d9b1101a6143fb8bce00e2eac4 # v2.7.1 + uses: step-security/harden-runner@17d0e2bd7d51742c71671bd19fa12bdc9d40a3d6 # v2.8.1 with: egress-policy: audit - - uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5 + - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 - name: Build the Docker image run: docker build -f "Dockerfile" --tag vprodemo.azurecr.io/rpc-go:${{ github.sha }} --tag vprodemo.azurecr.io/rpc-go:latest . - name: Docker Login if: ${{ github.ref == 'refs/heads/main' }} - uses: docker/login-action@e92390c5fb421da1463c202d546fed0ec5c39f20 # v3.1.0 + uses: docker/login-action@0d4c9c5ea7693da7b068278f7b52bda2a190a446 # v3.2.0 with: registry: vprodemo.azurecr.io username: ${{ secrets.DOCKER_USERNAME }} diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 04df9c71..9da31cf1 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -15,11 +15,11 @@ jobs: steps: - name: Harden Runner - uses: step-security/harden-runner@a4aa98b93cab29d9b1101a6143fb8bce00e2eac4 # v2.7.1 + uses: step-security/harden-runner@17d0e2bd7d51742c71671bd19fa12bdc9d40a3d6 # v2.8.1 with: egress-policy: audit - - uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5 + - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 - name: Setup .NET uses: actions/setup-dotnet@4d6c8fcf3c8f7a60068d26b594648e99df24cee3 # v4.0.0 with: diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index de3b42cd..5f6cd92b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -32,11 +32,11 @@ jobs: steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - name: Harden Runner - uses: step-security/harden-runner@a4aa98b93cab29d9b1101a6143fb8bce00e2eac4 # v2.7.1 + uses: step-security/harden-runner@17d0e2bd7d51742c71671bd19fa12bdc9d40a3d6 # v2.8.1 with: egress-policy: audit - - uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5 + - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 - name: Format if: ${{ matrix.os == 'ubuntu-20.04' }} run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi @@ -53,7 +53,7 @@ jobs: - name: run the tests with coverage run: go test ./... -coverprofile=coverage.out -covermode=atomic - - uses: codecov/codecov-action@6d798873df2b1b8e5846dba6fb86631229fbcb17 # v4.4.0 + - uses: codecov/codecov-action@125fc84a9a348dbcf27191600683ec096ec9021c # v4.4.1 if: ${{ matrix.os == 'ubuntu-20.04' }} name: Upload Coverage Results diff --git a/.github/workflows/projectsSync.yaml b/.github/workflows/projectsSync.yaml index 1c99bb8d..a575d326 100644 --- a/.github/workflows/projectsSync.yaml +++ b/.github/workflows/projectsSync.yaml @@ -14,7 +14,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden Runner - uses: step-security/harden-runner@a4aa98b93cab29d9b1101a6143fb8bce00e2eac4 # v2.7.1 + uses: step-security/harden-runner@17d0e2bd7d51742c71671bd19fa12bdc9d40a3d6 # v2.8.1 with: egress-policy: audit diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index cbf6c91c..8c96ba32 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -11,23 +11,28 @@ on: push: branches: [ main ] +permissions: + contents: read + jobs: release: + permissions: + contents: write # for Git to git push runs-on: ubuntu-latest steps: - name: Harden Runner - uses: step-security/harden-runner@a4aa98b93cab29d9b1101a6143fb8bce00e2eac4 # v2.7.1 + uses: step-security/harden-runner@17d0e2bd7d51742c71671bd19fa12bdc9d40a3d6 # v2.8.1 with: egress-policy: audit - name: Checkout repository - uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5 + uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 with: persist-credentials: false - name: Docker Login if: ${{ github.ref == 'refs/heads/main' }} - uses: docker/login-action@e92390c5fb421da1463c202d546fed0ec5c39f20 # v3.1.0 + uses: docker/login-action@0d4c9c5ea7693da7b068278f7b52bda2a190a446 # v3.2.0 with: registry: vprodemo.azurecr.io username: ${{ secrets.DOCKER_USERNAME }} @@ -54,7 +59,7 @@ jobs: echo "next=none" >> "$GITHUB_OUTPUT" fi - - uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5 + - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 if: ${{ steps.version.outputs.next != 'none' }} with: repository: open-amt-cloud-toolkit/e2e-testing diff --git a/.github/workflows/scorecard.yml b/.github/workflows/scorecard.yml index 002685bd..e51c8c24 100644 --- a/.github/workflows/scorecard.yml +++ b/.github/workflows/scorecard.yml @@ -32,12 +32,12 @@ jobs: steps: - name: Harden Runner - uses: step-security/harden-runner@a4aa98b93cab29d9b1101a6143fb8bce00e2eac4 # v2.7.1 + uses: step-security/harden-runner@17d0e2bd7d51742c71671bd19fa12bdc9d40a3d6 # v2.8.1 with: egress-policy: audit - name: "Checkout code" - uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5 + uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 with: persist-credentials: false @@ -72,6 +72,6 @@ jobs: # Upload the results to GitHub's code scanning dashboard. - name: "Upload to code-scanning" - uses: github/codeql-action/upload-sarif@b7cec7526559c32f1616476ff32d17ba4c59b2d6 # v2.1.27 + uses: github/codeql-action/upload-sarif@2e230e8fe0ad3a14a340ad0815ddb96d599d2aff # v2.1.27 with: sarif_file: results.sarif diff --git a/.github/workflows/semantic.yml b/.github/workflows/semantic.yml index 38f9a277..9907bbf5 100644 --- a/.github/workflows/semantic.yml +++ b/.github/workflows/semantic.yml @@ -9,11 +9,11 @@ jobs: runs-on: ubuntu-latest steps: - name: Harden Runner - uses: step-security/harden-runner@a4aa98b93cab29d9b1101a6143fb8bce00e2eac4 # v2.7.1 + uses: step-security/harden-runner@17d0e2bd7d51742c71671bd19fa12bdc9d40a3d6 # v2.8.1 with: egress-policy: audit - - uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5 + - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 with: fetch-depth: 0 - uses: wagoid/commitlint-github-action@7f0a61df502599e1f1f50880aaa7ec1e2c0592f2 # v6.0.1 diff --git a/.github/workflows/trivy-scan.yml b/.github/workflows/trivy-scan.yml index fdbae3b5..b82677c6 100644 --- a/.github/workflows/trivy-scan.yml +++ b/.github/workflows/trivy-scan.yml @@ -8,23 +8,24 @@ on: branches: [ main ] permissions: contents: read - security-events: write + jobs: build: runs-on: ubuntu-latest - + permissions: + security-events: write steps: - name: Harden Runner - uses: step-security/harden-runner@a4aa98b93cab29d9b1101a6143fb8bce00e2eac4 # v2.7.1 + uses: step-security/harden-runner@17d0e2bd7d51742c71671bd19fa12bdc9d40a3d6 # v2.8.1 with: egress-policy: audit - - uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5 + - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 - name: Build the Docker image run: docker build . --file Dockerfile --tag vprodemo.azurecr.io/rpc-go:${{ github.sha }} --tag vprodemo.azurecr.io/rpc-go:latest - name: Run Trivy vulnerability scanner - uses: aquasecurity/trivy-action@b2933f565dbc598b29947660e66259e3c7bc8561 # master + uses: aquasecurity/trivy-action@595be6a0f6560a0a8fc419ddf630567fc623531d # master with: image-ref: 'vprodemo.azurecr.io/rpc-go:${{ github.sha }}' format: 'sarif' @@ -34,7 +35,7 @@ jobs: vuln-type: 'os,library' severity: 'UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL' - name: Upload Trivy scan results to GitHub Security tab - uses: github/codeql-action/upload-sarif@b7cec7526559c32f1616476ff32d17ba4c59b2d6 # v3.25.5 + uses: github/codeql-action/upload-sarif@2e230e8fe0ad3a14a340ad0815ddb96d599d2aff # v3.25.8 if: always() with: sarif_file: 'trivy-results.sarif' \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index c0854c70..dc2489b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [2.34.2](https://github.com/open-amt-cloud-toolkit/rpc-go/compare/v2.34.1...v2.34.2) (2024-06-05) + + +### Bug Fixes + +* resolves close call issue in go-wsman-messages ([#550](https://github.com/open-amt-cloud-toolkit/rpc-go/issues/550)) ([af9ffdb](https://github.com/open-amt-cloud-toolkit/rpc-go/commit/af9ffdb9e1faae44a876346b9e08e67fb88e08b4)) + ## [2.34.1](https://github.com/open-amt-cloud-toolkit/rpc-go/compare/v2.34.0...v2.34.1) (2024-05-08) diff --git a/Dockerfile b/Dockerfile index 42ae83dd..d421878a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ # * SPDX-License-Identifier: Apache-2.0 # ********************************************************************** -FROM golang:1.22-alpine@sha256:2a882244fb51835ebbd8313bffee83775b0c076aaf56b497b43d8a4c72db65e1 as builder +FROM golang:1.22-alpine@sha256:9bdd5692d39acc3f8d0ea6f81327f87ac6b473dd29a2b6006df362bff48dd1f8 as builder RUN apk update RUN apk upgrade RUN apk add --no-cache git diff --git a/go.mod b/go.mod index a6d0a080..497b9dac 100644 --- a/go.mod +++ b/go.mod @@ -7,13 +7,13 @@ go 1.20 require ( github.com/google/uuid v1.6.0 - github.com/gorilla/websocket v1.5.1 + github.com/gorilla/websocket v1.5.2 github.com/hirochachacha/go-smb2 v1.1.0 github.com/ilyakaznacheev/cleanenv v1.5.0 - github.com/open-amt-cloud-toolkit/go-wsman-messages/v2 v2.5.2 + github.com/open-amt-cloud-toolkit/go-wsman-messages/v2 v2.8.0 github.com/sirupsen/logrus v1.9.3 github.com/stretchr/testify v1.9.0 - golang.org/x/sys v0.20.0 + golang.org/x/sys v0.21.0 ) require ( @@ -27,7 +27,7 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/joho/godotenv v1.5.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/term v0.20.0 + golang.org/x/term v0.21.0 gopkg.in/yaml.v3 v3.0.1 olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect software.sslmate.com/src/go-pkcs12 v0.4.0 diff --git a/go.sum b/go.sum index 0e89c4ab..c79bf415 100644 --- a/go.sum +++ b/go.sum @@ -7,16 +7,16 @@ github.com/geoffgarside/ber v1.1.0 h1:qTmFG4jJbwiSzSXoNJeHcOprVzZ8Ulde2Rrrifu5U9 github.com/geoffgarside/ber v1.1.0/go.mod h1:jVPKeCbj6MvQZhwLYsGwaGI52oUorHoHKNecGT85ZCc= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= -github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= +github.com/gorilla/websocket v1.5.2 h1:qoW6V1GT3aZxybsbC6oLnailWnB+qTMVwMreOso9XUw= +github.com/gorilla/websocket v1.5.2/go.mod h1:0n9H61RBAcf5/38py2MCYbxzPIY9rOkpvvMT24Rqs30= github.com/hirochachacha/go-smb2 v1.1.0 h1:b6hs9qKIql9eVXAiN0M2wSFY5xnhbHAQoCwRKbaRTZI= github.com/hirochachacha/go-smb2 v1.1.0/go.mod h1:8F1A4d5EZzrGu5R7PU163UcMRDJQl4FtcxjBfsY8TZE= github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2lOrsHHvr4= github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= -github.com/open-amt-cloud-toolkit/go-wsman-messages/v2 v2.5.2 h1:FDZoYaUfqidDuybd3muerAscJonjSMzcU3J+Ayo7i6I= -github.com/open-amt-cloud-toolkit/go-wsman-messages/v2 v2.5.2/go.mod h1:jdnbKqKRs4AYG8H7RZF/ss45D93+CfGY7m8esTTbDTs= +github.com/open-amt-cloud-toolkit/go-wsman-messages/v2 v2.8.0 h1:4HlR1Mr1KELUPii2IXJ/WDS/j8bVA8aCjc30nJFF1rY= +github.com/open-amt-cloud-toolkit/go-wsman-messages/v2 v2.8.0/go.mod h1:Z/zRJrraqGMxVTAqVRKE2QgeySouZP2vwkCy9u8UYb0= 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/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= @@ -35,10 +35,10 @@ golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= -golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw= -golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= +golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= +golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA= +golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internal/amt/commands.go b/internal/amt/commands.go index 79c6bce2..7a0e10f9 100644 --- a/internal/amt/commands.go +++ b/internal/amt/commands.go @@ -42,6 +42,7 @@ type InterfaceSettings struct { DHCPEnabled bool `json:"dhcpEnabled"` DHCPMode string `json:"dhcpMode"` IPAddress string `json:"ipAddress"` //net.IP + OsIPAddress string `json:"osIpAddress"` MACAddress string `json:"macAddress"` } @@ -351,6 +352,7 @@ func (amt AMTCommand) GetLANInterfaceSettings(useWireless bool) (InterfaceSettin settings := InterfaceSettings{ IPAddress: "0.0.0.0", + OsIPAddress: "0.0.0.0", IsEnabled: result.Enabled == 1, DHCPEnabled: result.DhcpEnabled == 1, LinkStatus: "down", diff --git a/internal/local/amt/wsman.go b/internal/local/amt/wsman.go index 3ff76bc4..cbd73b5b 100644 --- a/internal/local/amt/wsman.go +++ b/internal/local/amt/wsman.go @@ -215,7 +215,7 @@ func (g *GoWSMANMessages) GetCredentialRelationships() ([]credential.CredentialC if err != nil { return nil, err } - return response.Body.PullResponse.Items, nil + return response.Body.PullResponse.Items.CredentialContext, nil } func (g *GoWSMANMessages) GetConcreteDependencies() ([]concrete.ConcreteDependency, error) { response, err := g.wsmanMessages.CIM.ConcreteDependency.Enumerate() diff --git a/internal/local/info.go b/internal/local/info.go index e31f4161..2a23f052 100644 --- a/internal/local/info.go +++ b/internal/local/info.go @@ -6,10 +6,14 @@ package local import ( + "bytes" "encoding/json" + "errors" "fmt" + "net" "os" "rpc/internal/amt" + "rpc/internal/flags" "rpc/pkg/utils" "strconv" "strings" @@ -25,6 +29,63 @@ type PrivateKeyPairReference struct { AssociatedCerts []string } +func GetOSIPAddress(mac_addr string, netEnumerator flags.NetEnumerator) (string, error) { + mac_in_byte := make([]uint8, 6) + + mac_str := strings.Split(mac_addr, ":") + + for i, v := range mac_str { + value, _ := strconv.ParseUint(v, 16, 8) + mac_in_byte[i] = uint8(value) + } + interfaces, err := netEnumerator.Interfaces() + if err != nil { + return "0.0.0.0", errors.New("Failed to get net interfaces") + } + + if bytes.Equal(mac_in_byte, make([]byte, 6)) { + return "0.0.0.0", nil + } + + for _, iface := range interfaces { + if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 { + continue // interface down || loopback interface + } + + hwaddr := iface.HardwareAddr + + if bytes.Equal(hwaddr, mac_in_byte) { + addrs, err := netEnumerator.InterfaceAddrs(&iface) + if err != nil { + return "0.0.0.0", errors.New("Failed to get interface addresses") + } + + for _, addr := range addrs { + var ip net.IP + + switch v := addr.(type) { + case *net.IPNet: + ip = v.IP + case *net.IPAddr: + ip = v.IP + } + + // Check if the IP address is not nil and is an IPv4 address + if ip == nil || ip.IsLoopback() { + continue + } + ip = ip.To4() + if ip == nil { + continue // not an ipv4 address + } + + return ip.String(), nil + } + } + } + return "Not Found", nil +} + func (service *ProvisioningService) DisplayAMTInfo() (err error) { dataStruct := make(map[string]interface{}) cmd := service.amtCommand @@ -158,6 +219,22 @@ func (service *ProvisioningService) DisplayAMTInfo() (err error) { if err != nil { log.Error(err) } + + netEnumerator := flags.NetEnumerator{ + Interfaces: func() ([]net.Interface, error) { + return net.Interfaces() + }, + InterfaceAddrs: func(iface *net.Interface) ([]net.Addr, error) { + return iface.Addrs() + }, + } + + wired_osIpAddress, err := GetOSIPAddress(wired.MACAddress, netEnumerator) + if err != nil { + log.Error(err) + } + wired.OsIPAddress = wired_osIpAddress + dataStruct["wiredAdapter"] = wired if wired.MACAddress != "00:00:00:00:00:00" { @@ -165,7 +242,8 @@ func (service *ProvisioningService) DisplayAMTInfo() (err error) { service.PrintOutput("DHCP Enabled : " + strconv.FormatBool(wired.DHCPEnabled)) service.PrintOutput("DHCP Mode : " + wired.DHCPMode) service.PrintOutput("Link Status : " + wired.LinkStatus) - service.PrintOutput("IP Address : " + wired.IPAddress) + service.PrintOutput("AMT IP Address : " + wired.IPAddress) + service.PrintOutput("OS IP Address : " + wired.OsIPAddress) service.PrintOutput("MAC Address : " + wired.MACAddress) } @@ -173,13 +251,21 @@ func (service *ProvisioningService) DisplayAMTInfo() (err error) { if err != nil { log.Error(err) } + + wireless_osIpAddress, err := GetOSIPAddress(wireless.MACAddress, netEnumerator) + if err != nil { + log.Error(err) + } + wireless.OsIPAddress = wireless_osIpAddress + dataStruct["wirelessAdapter"] = wireless service.PrintOutput("---Wireless Adapter---") service.PrintOutput("DHCP Enabled : " + strconv.FormatBool(wireless.DHCPEnabled)) service.PrintOutput("DHCP Mode : " + wireless.DHCPMode) service.PrintOutput("Link Status : " + wireless.LinkStatus) - service.PrintOutput("IP Address : " + wireless.IPAddress) + service.PrintOutput("AMT IP Address : " + wireless.IPAddress) + service.PrintOutput("OS IP Address : " + wireless.OsIPAddress) service.PrintOutput("MAC Address : " + wireless.MACAddress) } diff --git a/internal/local/info_test.go b/internal/local/info_test.go index 65e6c54f..e354ab42 100644 --- a/internal/local/info_test.go +++ b/internal/local/info_test.go @@ -7,6 +7,7 @@ package local import ( "errors" + "net" "rpc/internal/flags" "rpc/pkg/utils" "testing" @@ -181,3 +182,85 @@ func TestDecodeAMT(t *testing.T) { } } } + +var testNetEnumerator1 = flags.NetEnumerator{ + Interfaces: func() ([]net.Interface, error) { + return []net.Interface{ + { + Index: 0, MTU: 1200, Name: "ethTest01", + HardwareAddr: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}, + Flags: 1, + }, + }, nil + }, + InterfaceAddrs: func(i *net.Interface) ([]net.Addr, error) { + if i.Name == "errTest01" { + return nil, errors.New("test message") + } else { + return []net.Addr{ + &net.IPNet{ + IP: net.ParseIP("127.0.0.1"), + Mask: net.CIDRMask(8, 32), + }, + &net.IPNet{ + IP: net.ParseIP("192.168.1.1"), + Mask: net.CIDRMask(24, 32), + }, + }, nil + } + }, +} + +var testNetEnumerator2 = flags.NetEnumerator{ + Interfaces: func() ([]net.Interface, error) { + return []net.Interface{ + { + Index: 0, MTU: 1200, Name: "errTest01", + HardwareAddr: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}, + Flags: 1, + }, + }, nil + }, + InterfaceAddrs: func(i *net.Interface) ([]net.Addr, error) { + if i.Name == "errTest01" { + return nil, errors.New("test message") + } else { + return []net.Addr{ + &net.IPNet{ + IP: net.ParseIP("127.0.0.1"), + Mask: net.CIDRMask(8, 32), + }, + &net.IPNet{ + IP: net.ParseIP("192.168.1.1"), + Mask: net.CIDRMask(24, 32), + }, + }, nil + } + }, +} + +func TestGetOSIPAddress(t *testing.T) { + t.Run("Valid MAC address", func(t *testing.T) { + osIpAddress, err := GetOSIPAddress("00:01:02:03:04:05", testNetEnumerator1) + assert.NoError(t, err) + assert.Equal(t, "192.168.1.1", osIpAddress) + }) + + t.Run("Zero MAC address", func(t *testing.T) { + osIpAddress, err := GetOSIPAddress("00:00:00:00:00:00", testNetEnumerator1) + assert.NoError(t, err) + assert.Equal(t, "0.0.0.0", osIpAddress) + }) + + t.Run("net interface fail", func(t *testing.T) { + osIpAddress, err := GetOSIPAddress("00:01:02:03:04:05", testNetEnumerator2) + assert.Equal(t, "0.0.0.0", osIpAddress) + assert.Equal(t, errors.New("Failed to get interface addresses"), err) + }) + + t.Run("no matching mac address to map into os ipaddress", func(t *testing.T) { + osIpAddress, err := GetOSIPAddress("00:11:22:33:44:55", testNetEnumerator1) + assert.Equal(t, "Not Found", osIpAddress) + assert.NoError(t, err) + }) +}