This repository has been archived by the owner on Oct 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ssh topology scanner initial implementation (#557)
* ssh topology scanner initial implementation * add missing licenses * fix some lint issues * remove bad unit test * change infofinders to infofinder * singular InfoType * singular InfoType * avoid using buffered channels * fix some typos * update vmclarity-tools-base * set root home if exists when getting home user dirs
- Loading branch information
Showing
30 changed files
with
1,012 additions
and
5 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
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
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
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,97 @@ | ||
// Copyright © 2023 Cisco Systems, Inc. and its affiliates. | ||
// All rights reserved. | ||
// | ||
// 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 infofinder | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/openclarity/kubeclarity/shared/pkg/job_manager" | ||
"github.com/openclarity/kubeclarity/shared/pkg/utils" | ||
|
||
"github.com/openclarity/vmclarity/pkg/shared/families/infofinder/job" | ||
infofinderTypes "github.com/openclarity/vmclarity/pkg/shared/families/infofinder/types" | ||
"github.com/openclarity/vmclarity/pkg/shared/families/interfaces" | ||
"github.com/openclarity/vmclarity/pkg/shared/families/results" | ||
"github.com/openclarity/vmclarity/pkg/shared/families/types" | ||
familiesutils "github.com/openclarity/vmclarity/pkg/shared/families/utils" | ||
"github.com/openclarity/vmclarity/pkg/shared/log" | ||
) | ||
|
||
type InfoFinder struct { | ||
conf infofinderTypes.Config | ||
} | ||
|
||
func (i InfoFinder) Run(ctx context.Context, _ *results.Results) (interfaces.IsResults, error) { | ||
logger := log.GetLoggerFromContextOrDiscard(ctx).WithField("family", "info finder") | ||
logger.Info("InfoFinder Run...") | ||
|
||
infoFinderResults := NewResults() | ||
|
||
manager := job_manager.New(i.conf.ScannersList, i.conf.ScannersConfig, logger, job.Factory) | ||
for _, input := range i.conf.Inputs { | ||
startTime := time.Now() | ||
managerResults, err := manager.Run(utils.SourceType(input.InputType), input.Input) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to scan input %q for info: %v", input.Input, err) | ||
} | ||
endTime := time.Now() | ||
inputSize, err := familiesutils.GetInputSize(input) | ||
if err != nil { | ||
logger.Warnf("Failed to calculate input %v size: %v", input, err) | ||
} | ||
|
||
// Merge results. | ||
for name, result := range managerResults { | ||
logger.Infof("Merging result from %q", name) | ||
if assetScan, ok := result.(*infofinderTypes.ScannerResult); ok { | ||
if familiesutils.ShouldStripInputPath(input.StripPathFromResult, i.conf.StripInputPaths) { | ||
assetScan = stripPathFromResult(assetScan, input.Input) | ||
} | ||
infoFinderResults.AddScannerResult(assetScan) | ||
} else { | ||
return nil, fmt.Errorf("received bad scanner result type %T, expected infofinderTypes.ScannerResult", result) | ||
} | ||
} | ||
infoFinderResults.Metadata.InputScans = append(infoFinderResults.Metadata.InputScans, types.CreateInputScanMetadata(startTime, endTime, inputSize, input)) | ||
} | ||
|
||
logger.Info("InfoFinder Done...") | ||
|
||
return infoFinderResults, nil | ||
} | ||
|
||
// stripPathFromResult strip input path from results wherever it is found. | ||
func stripPathFromResult(result *infofinderTypes.ScannerResult, path string) *infofinderTypes.ScannerResult { | ||
for i := range result.Infos { | ||
result.Infos[i].Path = familiesutils.TrimMountPath(result.Infos[i].Path, path) | ||
} | ||
return result | ||
} | ||
|
||
func (i InfoFinder) GetType() types.FamilyType { | ||
return types.InfoFinder | ||
} | ||
|
||
// ensure types implement the requisite interfaces. | ||
var _ interfaces.Family = &InfoFinder{} | ||
|
||
func New(conf infofinderTypes.Config) *InfoFinder { | ||
return &InfoFinder{ | ||
conf: conf, | ||
} | ||
} |
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,28 @@ | ||
// Copyright © 2023 Cisco Systems, Inc. and its affiliates. | ||
// All rights reserved. | ||
// | ||
// 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 job | ||
|
||
import ( | ||
"github.com/openclarity/kubeclarity/shared/pkg/job_manager" | ||
|
||
"github.com/openclarity/vmclarity/pkg/shared/families/infofinder/sshtopology" | ||
) | ||
|
||
var Factory = job_manager.NewJobFactory() | ||
|
||
func init() { | ||
Factory.Register(sshtopology.ScannerName, sshtopology.New) | ||
} |
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,68 @@ | ||
// Copyright © 2023 Cisco Systems, Inc. and its affiliates. | ||
// All rights reserved. | ||
// | ||
// 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 infofinder | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/openclarity/vmclarity/pkg/shared/families/infofinder/types" | ||
familiestypes "github.com/openclarity/vmclarity/pkg/shared/families/types" | ||
) | ||
|
||
type FlattenedInfos struct { | ||
ScannerName string `json:"ScannerName"` | ||
types.Info | ||
} | ||
|
||
type Results struct { | ||
Metadata familiestypes.Metadata `json:"Metadata"` | ||
Infos []FlattenedInfos `json:"Infos"` | ||
} | ||
|
||
func NewResults() *Results { | ||
return &Results{ | ||
Metadata: familiestypes.Metadata{ | ||
Timestamp: time.Now(), | ||
Scanners: []string{}, | ||
}, | ||
Infos: []FlattenedInfos{}, | ||
} | ||
} | ||
|
||
func (*Results) IsResults() {} | ||
|
||
func (r *Results) addScannerNameToMetadata(name string) { | ||
for _, scannerName := range r.Metadata.Scanners { | ||
if scannerName == name { | ||
return | ||
} | ||
} | ||
r.Metadata.Scanners = append(r.Metadata.Scanners, name) | ||
} | ||
|
||
func (r *Results) AddScannerResult(scannerResult *types.ScannerResult) { | ||
r.addScannerNameToMetadata(scannerResult.ScannerName) | ||
|
||
for _, info := range scannerResult.Infos { | ||
r.Infos = append(r.Infos, FlattenedInfos{ | ||
ScannerName: scannerResult.ScannerName, | ||
Info: info, | ||
}) | ||
} | ||
|
||
// bump the timestamp as there are new results | ||
r.Metadata.Timestamp = time.Now() | ||
} |
Oops, something went wrong.