Lots of Morays here in Hawaii. Unless you really, really know what you are doing, look, but don't touch. Do not get "in their face".
Yes, people get "attacked" by morays. But I think a better word is "defensive responce" or "feeding behavior" rather than an attack. Like many have mentioned, they have poor eyesight, are probably pretty territorial, and likely cannot distinguish a friendly diver trying to pet them from a predator trying to eat them.
Years ago I had an "encounter" with a very aggressive moray while spear fishing. We were free diving, in 20-30 feet of water off the Kona coast. I was pulling a stringer with speared fish. The moray came out of the reef, and began a chase of the fish scent. He could not be convinced to retreat, was too aggressive, and sadly ended up on the stringer too. Again, this was not an "attack" by the moray on the diver, it was "feeding behavior"
Best wishes.