Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eval command support #423

Draft
wants to merge 21 commits into
base: support_admin_netpolicy
Choose a base branch
from

Conversation

shireenf-ibm
Copy link
Contributor

@shireenf-ibm shireenf-ibm commented Oct 20, 2024

#380 adding support to eval command
issue #443

  1. updated funcs related to eval command under eval pkg (for my convenience, i have separated the funcs of check.go file into 2 files, the new file is check_eval.go; all the related funcs to eval command (checking if specific conn is allowed) are there
  2. successfully running unit-tests from parsed resources in eval_test.go
  1. added new command-line tests :eval command with tests with ANPs (added relevant support for new policies (anps+banp) + for pods that are actually replica of a workload object that is not defined as Pod in the yamls (e.g. StatefulSet))

@shireenf-ibm shireenf-ibm marked this pull request as draft October 20, 2024 14:31
@shireenf-ibm shireenf-ibm changed the title eval command support w.i.p eval command support Oct 21, 2024
@shireenf-ibm shireenf-ibm changed the title w.i.p eval command support eval command support Oct 21, 2024
@shireenf-ibm shireenf-ibm marked this pull request as ready for review October 30, 2024 12:40
@shireenf-ibm shireenf-ibm marked this pull request as draft October 30, 2024 12:40
Copy link
Collaborator

@adisos adisos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initial comments

pkg/netpol/eval/internal/k8s/adminnetpol.go Outdated Show resolved Hide resolved
pkg/netpol/eval/internal/k8s/adminnetpol.go Outdated Show resolved Hide resolved
Comment on lines 148 to 150
// analyzeANPCapturedRes when an admin-network-policy captures a connection , its result may be Allow (final- allowed conn),
// or Deny (final - denied conn) or Pass (to be determined by netpol/ banp)
func analyzeANPCapturedRes(anpRes int) (allowedOrDenied, pass bool, err error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the function name and its description is confusing... isn't it about checking if the result means allow by the ANP?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the function name and return arg names are still confusing.. maybe consider a separate type for capturing all 3 options (allow/deny/pass) of return value ? instead of splitting into two return args, where sometimes their result is not relevant...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried to rename the returned values same as the return values of its calling func and added some documentation;
is this better?

this func is a helper that returns same values as its calling func allowedXgressConnectionByAdminNetpols
it is used only to avoid duplicate in the calling func.

pkg/netpol/eval/check_eval.go Outdated Show resolved Hide resolved
pkg/netpol/eval/check_eval.go Outdated Show resolved Hide resolved
pkg/netpol/eval/check_eval.go Outdated Show resolved Hide resolved
// anpPortContains returns if the given AdminNetworkPolicyPort selects the input connection
//
//gocyclo:ignore
func anpPortContains(rulePorts *[]apisv1a.AdminNetworkPolicyPort, protocol, port string, dst Peer) (bool, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this function have some code used also in func (np *NetworkPolicy) ruleConnsContain that can be captured in helper functions instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, done

pkg/cli/evaluate.go Outdated Show resolved Hide resolved
Comment on lines 104 to 118
case parser.ReplicaSet:
err = pe.InsertObject(obj.ReplicaSet)
case parser.Deployment:
err = pe.InsertObject(obj.Deployment)
case parser.DaemonSet:
err = pe.InsertObject(obj.DaemonSet)
case parser.StatefulSet:
err = pe.InsertObject(obj.StatefulSet)
case parser.ReplicationController:
err = pe.InsertObject(obj.ReplicationController)
case parser.Job:
err = pe.InsertObject(obj.Job)
case parser.CronJob:
err = pe.InsertObject(obj.CronJob)
// ns kind
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if eval only supports pods, why adding these resources?
If the dir path only contains workload manifests and not pods, how would one infer the pod name for this command?
maybe this should be a separate issue, to extend eval support for workloads, but in this PR can just focus on pods?
the tests automation can add the pods resources and adjust the input, as if those manifests would exist for testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure understood last sentence;
my tests dir paths contain manifests with StatefulSet workloads (I used replica name as the input of the test (pod name);
The tests failed without adding these cases (my manifests were not inserted to the policy-engine because of the lack of these cases; );
should I revert these changes and create new tests with pods only in the manifests?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for these tests to work, you can add test automation (for eval tests) that add pods resources as if they existed in the manifests.. the changes here better be removed, until eval actually supports such resources, because in the current eval usage workload resources are not supported, and a user cannot infer the pod name to be used in this command when providing only workload resources.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modified and added tests:

  1. command_test.go : since command-line needs the "dir" as an argument to execute the cmd;
    for those tests (which don't contain pods in the manifests), I've generated a "temp dir" which is a copy of the original dir + generated yamls for pods (for the given src + dst arguments) so the command runs on the "temporary dir" successfully.
    generating pod yaml for a workload considers also its labels (which may be used in the netpols rules and are essential for computing the result);

  2. eval_test.go uses the internal eval funcs, so it works successfully with the original dirs.
    in this file we had tests from parsed resources (that tests the "eval" functionality) ;
    and I have added eval tests from dir paths.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, please open a separate issue to consider supporting eval with input workloads, not just pods, and also add a comment for this test functionality added here, that it can be removed when this eval extension is added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, new issue #440 ; + comments were added in the relevant files

Comment on lines 1934 to 1941
dir: "anp_banp_core_test",
sourceNs: "network-policy-conformance-gryffindor",
sourcePod: "harry-potter",
destPod: "cedric-diggory",
destNs: "network-policy-conformance-hufflepuff",
port: "8080",
evalResult: true,
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where are the pod names coming from?
the test dir manifests do not contain pod resources.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the names are of the relevant workloads in the manifests.
I mentioned that the input-dir does not contain pods so it will generate a pod matching the workload resource with the src + dst names.
e.g. the generated src pod takes the metadata of the workload with the sourcePod param name. (i.w. its name, namespace and labels)

Copy link
Collaborator

@adisos adisos Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, so the test attribute should be srcWorkload or dstWorkload, and also better add some documentation explaining that for these tests eval will be called with pod names owned by these workloads (pods which are added by policy engine ).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

protocol string
port string
evalResult bool
generateManifests bool // indicates if the test dir does not contain pods - to be generated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename generateManifests to generatePodManifests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


const (
tmpPattern = "temp-*"
exeMode = 0o777
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't 600 sufficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, done

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i put 600 for file access and 700 for creating the dir ( 600 on all failed)

// copyDir copies files of network-policies from origDir into tempDir
// and generates into the tempDir : Pod yaml files for given src and dst peers from their workload resources
// in the origDir
func copyDir(origDir, tempDir, srcName, srcNs, dstName, dstNs string) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider renaming copyDir to something like copyAndExtendDir or copyDirAndAddPods

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return "", osErr
}
}
// making temp directory in the temp path
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this required? isn't it sufficient to just use TmpDir ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, done

Comment on lines 161 to 169
// copyFile copies origFile to tempFile
func copyFile(origFile, tempFile string) error {
contents, err := os.ReadFile(origFile)
if err != nil {
return err
}
err = os.WriteFile(tempFile, contents, exeMode)
return err
}
Copy link
Collaborator

@adisos adisos Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there an os func that can be used instead of this entire copyFile function?

Copy link
Contributor Author

@shireenf-ibm shireenf-ibm Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't met such func that I can use to copy files in a more efficient or "shorter" way

Comment on lines +125 to +135
// Splitting the YAML into multiple documents
docs := splitYamlDocs(fileContents)

// we are interested in Metadata of the workload only.
// Iterate through objects to find the matching one
for _, doc := range docs {
var obj WorkloadMetadata
if err := yaml.Unmarshal(doc, &obj); err != nil {
return nil, err
}
if obj.Metadata.Name == podName && (obj.Metadata.Namespace == podNs ||
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can use instead the already existing functions from manifests.GetResourceInfosFromDirPath() and
parser.ResourceInfoListToK8sObjectsList() ?

Copy link
Contributor Author

@shireenf-ibm shireenf-ibm Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we may use these funcs but I think that the written solution without using them is shorter and more efficient for my goal.

  1. I need to copy the yaml files to the temp dir (as it is the arg of the command); in this case those funcs may not help and i will have to walk the dir anyway.

  2. I don't need to convert all objects in the dir to k8s objects, I just want the specific workload resources of the src and dst (just 2 objects of many converted - I have nothing to do with k8s objects - they are not the args of the cmd;)

  3. I can not send those funcs only the workload files (assuming we can indicate what files are those; and ignore copying these files);

  • the file with the workload resource may also contain Namespace object and I must have this yaml in the output; because Ns labels may be used as selectors in the policy rules.

@shireenf-ibm shireenf-ibm linked an issue Nov 12, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

eval command : support (Baseline)AdminNetworkPolicy
2 participants