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

Implemented and Tested ReadPublic #279

Merged
merged 3 commits into from
May 26, 2022
Merged

Conversation

matt-tsai
Copy link
Contributor

Implemented and Tested ReadPublic

@matt-tsai matt-tsai requested a review from a team as a code owner May 25, 2022 21:34
Copy link
Member

@chrisfenner chrisfenner left a comment

Choose a reason for hiding this comment

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

Thanks for the change! This looks pretty solid, I just have some minor notes (mostly about the test)

},
},
},
}
Copy link
Member

Choose a reason for hiding this comment

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

Can we defer a FlushContext similar to what we do here:

cleanup := func() {
t.Helper()
flush := FlushContext{
FlushHandle: rsp.ObjectHandle,
}
if err := flush.Execute(thetpm); err != nil {
t.Errorf("could not flush signing key: %v", err)
}
}

Something like:

defer func() { 
 	flush := FlushContext{ 
 		FlushHandle: rsp.ObjectHandle, 
 	} 
 	if err := flush.Execute(thetpm); err != nil { 
 		t.Errorf("could not flush signing key: %v", err) 
 	} 
 }()

Copy link
Contributor

Choose a reason for hiding this comment

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

@chrisfenner
Sorry to make this more convoluted. Common commands like FlushContext take up more lines of code than go-tpm/tpm2 currently does. Users likely wouldn't want the clutter of management commands like FlushContext taking too much space; they care about the business logic of what they're putting into commands like CreatePrimary or ReadPublic.

Would having the idiom below be easier to read?

defer func() {
  if err := FlushContext{FlushHandle: rsp.ObjectHandle}.Execute(thetpm); err != nil {
    t.Errorf("...")
  }
}

we could also make wrappers for commands like FlushContext, although I think it seems unnecessary for a small command like FlushContext.
e.g. in a util pkg or something:

direct/tpm2/directutil/context
func FlushContext(t transport.TPM, flushHandle handle) error {
  return FlushContext{ FlushHandle: flushHandle}.Execute(t)
}

direct/tpm2/read_public_test.go:
defer func() {
  if err := directutil.FlushContext(thetpm, rsp.ObjectHandle); err != nil {
    t.Errorf("...")
  }
}

Copy link
Member

Choose a reason for hiding this comment

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

We discussed offline - we don't have to assert that every flush succeeded during the tests (the current tests don't; we just need a FlushContext test that checks it once).

So, I'd like to update my suggestion to just: can we add

defer &FlushContext{rsp.ObjectHandle}.Execute(thetpm))

here?

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 was getting
function must be invoked in defer statement for

defer &FlushContext{rsp.ObjectHandle}.Execute(thetpm))

So my work around was

flushContext := FlushContext{FlushHandle: rspCP.ObjectHandle}
defer flushContext.Execute(thetpm)

which seems to work just as well

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you try defer (&FlushContext{rsp.ObjectHandle}).Execute(thetpm))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The same error occurs.

Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with the 2 line version here, and if we think of a neat idiom later it's easy to change.

},
},
},
}
Copy link
Contributor

Choose a reason for hiding this comment

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

@chrisfenner
Sorry to make this more convoluted. Common commands like FlushContext take up more lines of code than go-tpm/tpm2 currently does. Users likely wouldn't want the clutter of management commands like FlushContext taking too much space; they care about the business logic of what they're putting into commands like CreatePrimary or ReadPublic.

Would having the idiom below be easier to read?

defer func() {
  if err := FlushContext{FlushHandle: rsp.ObjectHandle}.Execute(thetpm); err != nil {
    t.Errorf("...")
  }
}

we could also make wrappers for commands like FlushContext, although I think it seems unnecessary for a small command like FlushContext.
e.g. in a util pkg or something:

direct/tpm2/directutil/context
func FlushContext(t transport.TPM, flushHandle handle) error {
  return FlushContext{ FlushHandle: flushHandle}.Execute(t)
}

direct/tpm2/read_public_test.go:
defer func() {
  if err := directutil.FlushContext(thetpm, rsp.ObjectHandle); err != nil {
    t.Errorf("...")
  }
}

@chrisfenner
Copy link
Member

Not at all @alexmwu, I totally agree with the feedback. It would be awesome to have a one-line idiom for "defer flush this thing" and even awesomer to have a test version that asserts that the flush succeeded.

I'm in email/meeting mode all day today so I can't remember if this would compile but can we have something along the lines of

defer t.Run("🚽", &FlushContext{rsp.ObjectHandle}.Execute(thetpm) == nil)

in the tests and

defer &FlushContext{rsp.ObjectHandle}.Execute(thetpm))

in sample code?

If that's not possible with the current reflection logic, are there tweaks we could make to make it possible?

},
},
},
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with the 2 line version here, and if we think of a neat idiom later it's easy to change.

@chrisfenner chrisfenner merged commit 0e74fce into google:tpmdirect May 26, 2022
@matt-tsai matt-tsai deleted the readPublic branch May 26, 2022 18:07
matt-tsai added a commit to matt-tsai/go-tpm that referenced this pull request Jun 22, 2022
* Implemented and Tested ReadPublic

* Fixed Comment Style for ReadPublic

* Resolved most changes requested for PR

Co-authored-by: Matthew Tsai <[email protected]>
@matt-tsai matt-tsai changed the title Read public Implemented and Tested ReadPublic Jul 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants